Difference between revisions of "Matlab cell array"

From Dynamo
Jump to navigation Jump to search
Line 32: Line 32:
  
 
=== Loops on models ===
 
=== Loops on models ===
 +
Operations on sets of models are frequently more straightforward when executed through Matlab/''Dynamo'' console scripts rather than in the different GUIs. For instance, processing  [[membrane models]], you frequently use the commands [[membrane models#Command line]].
  
 
+
In a typical situation, you'd want to use a script that loops on a series of predefined models contained in a cell array (created manually or through some ''Dynamo'' tool to [[Matlab cell array#Extracting model files from a catalogue extract model sets from a catalogue]]) 
  
 
=== Extracting model files from a catalogue ===  
 
=== Extracting model files from a catalogue ===  
 
The names of the model files in a catalogue can be easily extracted with the command line <tt>dcm</tt> utility. In the command:
 
The names of the model files in a catalogue can be easily extracted with the command line <tt>dcm</tt> utility. In the command:
  
  <tt>dcm myCatalogue -i * -l m -ws o </tt>
+
  <tt>dcm -c testCatalogue -i * -l m -ws o </tt>
  
 
the flags have the meaning:
 
the flags have the meaning:
* <tt>-i * </tt> : visit all (<tt>*</tt>) volume indices (<tt>-i</tt>)
+
* <tt>-i * </tt> : visit all (<tt>*</tt>) volume indices (<tt>-i</tt>). This is optional.
* <tt>-l m </tt>: on each visited volume (<tt>-l</tt>) list all the models (<tt>m</tt>)
+
* <tt>-l m </tt>: on each visited volume, list  (<tt>-l</tt>) all the models (<tt>m</tt>)
 
* <tt>-ws o </tt>: put the results in a workspace variable (<tt>-ws</tt>) arbitrarily called <tt>o</tt>
 
* <tt>-ws o </tt>: put the results in a workspace variable (<tt>-ws</tt>) arbitrarily called <tt>o</tt>
  
so that the order will show  on screen the names of all the models inside the catalogue <tt>myCatalogue</tt>.
+
so that the order will show  on screen the names of all the models inside the catalogue <tt>myCatalogue</tt> and puts them into the cellarray <tt>o.results</tt>. You can see them by:
 +
 
 +
<tt>mbdisp(o.results);</tt>
 +
 
 +
The object <tt>o.results</tt> is a cell array and you can use loops that iterate through it.
 +
 
 +
Note that you can grep model files  from only selected volume indices:
 +
 
 +
<tt>dcm -c testCatalogue -l m -ws o -i [1,3]</tt>
 +
 
 +
With some practice in parsing strings in Matlab, you can extract a new cell array from a previous cell array, greping only the cell array elements that include some given string in their name. Thus, it is convenient to have a sensible naming convention while you create your models inside the tomograms, as it will facilitate accessing them if required on a later stage. <br>
 +
 
 +
Note that elements in the cell array extracted from the catalogue <tt>myCatalogue</tt> with this command are not model objects. They are just strings, each one defining a the name of a model file. If you want to bring the models into the memory to operate on them you need to read the files.
 +
 
 +
<tt>myModels = dread(o.results); </tt>
 +
 
 +
will create a cell array called <tt>myModels</tt>, where each element is result of reading one model file. Alternatively, you can read them separately, or read one at a time:
 +
 
 +
<tt>oneModel = dread(o.results{2}); </tt>
 +
 
 +
would go to the cellarray <tt>o.results</tt>, fetch the element in position 2 (which is a string), read the file named as that string, and create in memory the object <tt>oneModel</tt>, which should be a 'live' model object that can be edited.

Revision as of 13:05, 2 February 2017

The cell array is a Matlab container for the storage of series of objects in the memory workspace. Individuals elements are accessed or created with the curly bracket {} notation. Cell arrays are available both in Matlab and in the standalone versions of Dynamo.


Basic syntax

Some basic examples:

a = {};

creates an empty cell array called a in the memory workspace.

a = {'hello','bye'};

creates a cell array that contains the strings hello and bye, so that

disp(a{1}); will just write the string hello in the Matlab desktop ( or in the Dynamo standalone console).

Individual elements can be added through the {}notation:

a{3} = 'hi again';

will enlarge the cell array with a third element.


A complete (and rather lengthy) description can be found in the |matlab page

Cell arrays in Dynamo

Cell arrays are frequently of interest for Dynamo users that need to perform operations that are complicated or not available through the GUIs.

Loops on models

Operations on sets of models are frequently more straightforward when executed through Matlab/Dynamo console scripts rather than in the different GUIs. For instance, processing membrane models, you frequently use the commands membrane models#Command line.

In a typical situation, you'd want to use a script that loops on a series of predefined models contained in a cell array (created manually or through some Dynamo tool to Matlab cell array#Extracting model files from a catalogue extract model sets from a catalogue)

Extracting model files from a catalogue

The names of the model files in a catalogue can be easily extracted with the command line dcm utility. In the command:

dcm -c testCatalogue -i * -l m -ws o 

the flags have the meaning:

  • -i *  : visit all (*) volume indices (-i). This is optional.
  • -l m : on each visited volume, list (-l) all the models (m)
  • -ws o : put the results in a workspace variable (-ws) arbitrarily called o

so that the order will show on screen the names of all the models inside the catalogue myCatalogue and puts them into the cellarray o.results. You can see them by:

mbdisp(o.results);

The object o.results is a cell array and you can use loops that iterate through it.

Note that you can grep model files from only selected volume indices:

dcm -c testCatalogue -l m -ws o -i [1,3]

With some practice in parsing strings in Matlab, you can extract a new cell array from a previous cell array, greping only the cell array elements that include some given string in their name. Thus, it is convenient to have a sensible naming convention while you create your models inside the tomograms, as it will facilitate accessing them if required on a later stage.

Note that elements in the cell array extracted from the catalogue myCatalogue with this command are not model objects. They are just strings, each one defining a the name of a model file. If you want to bring the models into the memory to operate on them you need to read the files.

myModels = dread(o.results);

will create a cell array called myModels, where each element is result of reading one model file. Alternatively, you can read them separately, or read one at a time:

oneModel = dread(o.results{2});

would go to the cellarray o.results, fetch the element in position 2 (which is a string), read the file named as that string, and create in memory the object oneModel, which should be a 'live' model object that can be edited.