<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to Usage Examples</title><link>https://sourceforge.net/p/csmatio/wiki/Usage%2520Examples/</link><description>Recent changes to Usage Examples</description><atom:link href="https://sourceforge.net/p/csmatio/wiki/Usage%20Examples/feed" rel="self"/><language>en</language><lastBuildDate>Tue, 07 Nov 2017 07:24:59 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/csmatio/wiki/Usage%20Examples/feed" rel="self" type="application/rss+xml"/><item><title>Usage Examples modified by Tobias</title><link>https://sourceforge.net/p/csmatio/wiki/Usage%2520Examples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v13
+++ v14
@@ -142,7 +142,7 @@

 ### Q: How to read Matlab structs? ###

-A: Reading of structs had a bug in the original 2007 version of David. Make sure you use the latest csmatio version.
+A: Reading from structs was buggy in the original 2007 version. Make sure you use the latest csmatio version.

 The csmatio source code contains a demo file named `"struct.mat"`. Here is some demo code to read the content of this mat file:

@@ -153,6 +153,12 @@

     // get a reference to the matlab struct named 'X' 
     MLStructure mlStruct = mfr.Content["X"] as MLStructure;
+
+    // print the names of the struct members (csmatio rev.18 or higher)
+    foreach (string key in mlStruct.Keys)
+    {
+        Console.WriteLine(key); // "var", "w", "Version"
+    }

     // get references to some struct member objects 
     MLChar mlVersion = mlStruct["Version"] as MLChar; 
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tobias</dc:creator><pubDate>Tue, 07 Nov 2017 07:24:59 -0000</pubDate><guid>https://sourceforge.net25d53eaa6de8f34a9c9be8c60133464f041626c3</guid></item><item><title>Usage Examples modified by Tobias</title><link>https://sourceforge.net/p/csmatio/wiki/Usage%2520Examples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v12
+++ v13
@@ -16,7 +16,7 @@
 ### Q: I know that the data is double array, how can I cast the MLArray type into MLDouble type? ###

-A: Consider you created a `"mydata.mat"` file containing a 1x10 double matrix using Matlab like this:
+A: Let's assume you were running Matlab and you created a `"mydata.mat"` file containing a 1x10 double matrix. You used these Matlab statements:

 ~~~~
 :::Matlab
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tobias</dc:creator><pubDate>Sun, 21 Jun 2015 10:43:35 -0000</pubDate><guid>https://sourceforge.net60e0a52b3fd32641e29f9fba8435643446cc97c2</guid></item><item><title>Usage Examples modified by Tobias</title><link>https://sourceforge.net/p/csmatio/wiki/Usage%2520Examples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v11
+++ v12
@@ -95,7 +95,7 @@
     data3x3[2] = new double[] { 300.0, 301.0, 302.0 }; // third row
 ~~~~

-From this you can create a Matlab double array named 'Matrix_3_by_3' with one line of code:
+From this you can create a Matlab double array named "Matrix_3_by_3" with one line of code:

 ~~~~
 :::C#
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tobias</dc:creator><pubDate>Sun, 21 Jun 2015 10:39:34 -0000</pubDate><guid>https://sourceforge.net7dbf45d36605be323d5888e81bf2162913621bb0</guid></item><item><title>Usage Examples modified by Tobias</title><link>https://sourceforge.net/p/csmatio/wiki/Usage%2520Examples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v10
+++ v11
@@ -44,7 +44,7 @@
             // create a reader for the file 
             MatFileReader mfr = new MatFileReader("mydata.mat");

-            // get a reference to out matlab 'squares' double matrix 
+            // get a reference to our matlab 'squares' double matrix 
             MLDouble mlSquares = (mfr.Content["squares"] as MLDouble); 
             if (mlSquares != null) 
             { 
@@ -82,9 +82,41 @@

+### Q: How to create a matlab 2D double array and save it to a mat file? ###
+
+A: There are several ways to do this. 
+Let's assume you've got the array values in a .NET double[][] array:
+
+~~~~
+:::C#
+    double[][] data3x3 = new double[3][];
+    data3x3[0] = new double[] { 100.0, 101.0, 102.0 }; // first row
+    data3x3[1] = new double[] { 200.0, 201.0, 202.0 }; // second row
+    data3x3[2] = new double[] { 300.0, 301.0, 302.0 }; // third row
+~~~~
+
+From this you can create a Matlab double array named 'Matrix_3_by_3' with one line of code:
+
+~~~~
+:::C#
+    MLDouble mlDoubleArray = new MLDouble("Matrix_3_by_3", data3x3);
+~~~~
+
+Now save this array to mat file "data.mat":
+
+~~~~
+:::C#
+    List&amp;lt;MLArray&amp;gt; mlList = new List&amp;lt;MLArray&amp;gt;(); 
+    mlList.Add(mlDoubleArray); 
+    MatFileWriter mfw = new MatFileWriter("data.mat", mlList, false);
+~~~~
+
+
+
+
 ### Q: How to create a 3D double array? ###

-A: You can easily create a 3D double array as follows:
+A: You can easily create an empty 3D double array as follows:

 ~~~~
 :::C#
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tobias</dc:creator><pubDate>Sun, 21 Jun 2015 10:21:43 -0000</pubDate><guid>https://sourceforge.net738aca6e235e9514309becd764a389c2b0f9f886</guid></item><item><title>Usage Examples modified by Tobias</title><link>https://sourceforge.net/p/csmatio/wiki/Usage%2520Examples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v9
+++ v10
@@ -1,5 +1,5 @@
 ### Q: How do I load data using this library? ###
-
+    
 A: First, you might want to get a reference to your matlab variable:

 ~~~~
@@ -199,3 +199,51 @@
     mlList.Add(structure); 
     MatFileWriter mfw = new MatFileWriter("data.mat", mlList, false);
 ~~~~
+
+Now lets create a **struct array**. First we initialize some data:
+
+~~~~
+:::C#
+    Score[] highscores = new Score[3];
+    int idx;
+    idx = 0;
+    highscores[idx] = new Score();
+    highscores[idx].Name = "Tom";
+    highscores[idx].Value = 41;
+    idx = 1;
+    highscores[idx] = new Score();
+    highscores[idx].Name = "Dick";
+    highscores[idx].Value = 43;
+    idx = 2;
+    highscores[idx] = new Score();
+    highscores[idx].Name = "Harry";
+    highscores[idx].Value = 47;
+~~~~
+
+Here is how you write this data into a mat file:
+
+~~~~
+:::C#
+    MLStructure mlStructArr = 
+        new MLStructure("highscores", new int[] { highscores.Length, 1 });
+    for (int i = 0; i &lt; highscores.Length; ++i)
+    {
+        mlStructArr["Name", i] = new MLChar("", highscores[i].Name);
+        mlStructArr["Value", i] = new MLDouble("", new double[] { highscores[i].Value }, 1);
+    }
+
+    // save to mat file using MatFileWriter
+    List mlList = new List(); 
+    mlList.Add(mlStructArr ); 
+    MatFileWriter mfw = new MatFileWriter("data.mat", mlList, false);
+~~~~
+
+In Matlab you can use the data in a number of ways:
+
+~~~~
+:::Matlab
+load('data.mat')
+winner = highscores(3); % { Name = 'Harry', Value = 47 }
+highscores(2).Name; % 'Dick'
+scoreValues = [highscores.Value]; % 41 43 47
+~~~~
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tobias</dc:creator><pubDate>Tue, 08 Apr 2014 15:49:55 -0000</pubDate><guid>https://sourceforge.net17d50f6afe095e8ba1a680cbd869045eaaa78a6f</guid></item><item><title>Usage Examples modified by Tobias</title><link>https://sourceforge.net/p/csmatio/wiki/Usage%2520Examples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v8
+++ v9
@@ -8,7 +8,7 @@
 MLArray mlArrayRetrieved = mfr.GetMLArray("my_array");
 ~~~~

-Second, you need to find out of which type `mlArrayRetrieved` really is (as `MLArray` is only a base class) and you cast into the real type. Depending on the real type there are different Methods/Properties that represent the data.
+Second, you need to find out of which type `mlArrayRetrieved` really is (as `MLArray` is only a base class) and you cast into the real type. Depending on the real type there are different methods/properties that represent the data.

@@ -62,7 +62,7 @@

 ### Q: How to read the content of a three-dimensional double array ? ###

-A: So lets assume we have a 220x180x33 double array called `"cube"` and we want to read what is in matlab syntax `"cube(7,18,29)"`:
+A: So lets assume we have a 220x180x33 double array called `"cube"` and we want to read what is in Matlab syntax `"cube(7,18,29)"`:

 ~~~~
 :::C#
@@ -161,7 +161,7 @@

 ### Q: I need to write some nested vars in .mat file ? ###

-A: Le me give you a basic example how to create nested matlab vars (aka structs) with this lib. Suppose we have following C# struct:
+A: Le me give you a basic example how to create nested Matlab vars (aka structs) with this lib. Suppose we have following C# struct:

 ~~~~
 :::C#
@@ -172,7 +172,7 @@
     }
 ~~~~

-Now we initialize an struct object named `highscore` of that type:
+Now we initialize a struct object named `highscore` of that type:

 ~~~~
 :::C#
@@ -181,7 +181,7 @@
     highscore.Value = 47.3;
 ~~~~

-Now we want to represent this struct object in the mat file:
+Here is how you represent this struct object in the mat file:

 ~~~~
 :::C#
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tobias</dc:creator><pubDate>Wed, 19 Mar 2014 08:57:20 -0000</pubDate><guid>https://sourceforge.netac9bb896b14a0fa1c4e79e83664caafd4fb4f350</guid></item><item><title>Usage Examples modified by Tobias</title><link>https://sourceforge.net/p/csmatio/wiki/Usage%2520Examples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tobias</dc:creator><pubDate>Mon, 24 Feb 2014 22:55:44 -0000</pubDate><guid>https://sourceforge.netc7089927134e960039e624abbbd0f3e22f2d09af</guid></item><item><title>UsageExamples modified by Tobias</title><link>https://sourceforge.net/p/csmatio/wiki/UsageExamples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v6
+++ v7
@@ -131,10 +131,31 @@
     double w = mlW.Get(0); // 3874.0
 ~~~~

-For reading Matlab struct arrays you can use 
-`mlStruct["Version", index]` instead of 
-`mlStruct["Version"].
-`
+
+
+
+### Q: I have a variable of type 1xN struct array with fields: spin(3x3). How do I get the data? ###
+
+A: Here is the example:
+
+~~~~
+:::C#
+    // create a reader for the file 
+    MatFileReader mfr = new MatFileReader("structarray.mat");
+
+    // get a reference to the matlab struct array named 's1' 
+    MLStructure mlStruct = mfr.Content["s1"] as MLStructure;
+
+    for (int i = 0; i &lt; mlStruct.Size; ++i) 
+    { 
+        // get reference to struct member 'spin' (3x3 double array) 
+        MLDouble mlSpin = mlStruct["spin", i] as MLDouble; 
+
+        // get values 
+        double[][] spin = mlSpin.GetArray(); 
+    }
+~~~~
+

&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tobias</dc:creator><pubDate>Mon, 24 Feb 2014 22:50:20 -0000</pubDate><guid>https://sourceforge.net7d9aa9e96d37417e8ce5715098a8338fce748558</guid></item><item><title>UsageExamples modified by Tobias</title><link>https://sourceforge.net/p/csmatio/wiki/UsageExamples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v5
+++ v6
@@ -131,6 +131,11 @@
     double w = mlW.Get(0); // 3874.0
 ~~~~

+For reading Matlab struct arrays you can use 
+`mlStruct["Version", index]` instead of 
+`mlStruct["Version"].
+`
+

 ### Q: I need to write some nested vars in .mat file ? ###
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tobias</dc:creator><pubDate>Mon, 24 Feb 2014 22:43:56 -0000</pubDate><guid>https://sourceforge.netf15cc02e1fe8c2ac4bd9a87ceba23fbe9caff0c2</guid></item><item><title>UsageExamples modified by Tobias</title><link>https://sourceforge.net/p/csmatio/wiki/UsageExamples/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v4
+++ v5
@@ -24,7 +24,7 @@
 save('mydata.mat', 'squares')
 ~~~~

-You can then use the following C# code to get this data into a .NET double-Array:
+**Complete example**: You can then use the following C# code to get this data into a .NET double array:

 ~~~~
 :::C#
@@ -108,9 +108,34 @@

+### Q: How to read Matlab structs? ###
+
+A: Reading of structs had a bug in the original 2007 version of David. Make sure you use the latest csmatio version.
+
+The csmatio source code contains a demo file named `"struct.mat"`. Here is some demo code to read the content of this mat file:
+
+~~~~
+:::C#
+    // create a reader for the file 
+    MatFileReader mfr = new MatFileReader("struct.mat");
+
+    // get a reference to the matlab struct named 'X' 
+    MLStructure mlStruct = mfr.Content["X"] as MLStructure;
+
+    // get references to some struct member objects 
+    MLChar mlVersion = mlStruct["Version"] as MLChar; 
+    MLDouble mlW = mlStruct["w"] as MLDouble;
+
+    // get values from struct members 
+    string version = mlVersion.GetString(0); // "1.0.5.23354" 
+    double w = mlW.Get(0); // 3874.0
+~~~~
+
+
+
 ### Q: I need to write some nested vars in .mat file ? ###

-A: Le me give you a basic example how to create nested matlab vars with this lib. Suppose we have following C# struct:
+A: Le me give you a basic example how to create nested matlab vars (aka structs) with this lib. Suppose we have following C# struct:

 ~~~~
 :::C#
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Tobias</dc:creator><pubDate>Mon, 24 Feb 2014 22:41:07 -0000</pubDate><guid>https://sourceforge.net8eea3ff006f048bcc33deec07b66d074846a5871</guid></item></channel></rss>