Difference between revisions of "Matlab cell array"

From Dynamo
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 36: Line 36:
 
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]]).
 
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]]).
  
  <nowiki> % myModels is a cellArray;
+
  <nowiki>% myModels is a cellArray;
for i=1:length(myModelFiles)
+
% creates a table for each model in a list of membrane models
 +
for i=1:length(myModelFiles)
 
     m = dread(myModelFiles{i}); % brings the model i into memory
 
     m = dread(myModelFiles{i}); % brings the model i into memory
 
     m.controlUpdate();  % creates the control points  
 
     m.controlUpdate();  % creates the control points  
     m. createMesh();  % creates the depiction mesh
+
     m.createMesh();  % creates the depiction mesh
 
     m.refineMesh();  % refines the depiction mesh
 
     m.refineMesh();  % refines the depiction mesh
 
     m.crop_mesh_parameter = 5;
 
     m.crop_mesh_parameter = 5;
Line 79: Line 80:
  
 
=== Extracting model files from a catalogue ===  
 
=== Extracting model files from a catalogue ===  
 +
 +
==== Through dcm ====
 
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:
  
Line 109: Line 112:
  
 
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.
 
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.
 +
 +
====Through dcmodels====
 +
 +
The <tt>dynamo_catalogue_models</tt> offers a simplified syntax and some more options for retrieval of model files:
 +
 +
<tt>files = dcmodels('testCatalogue','i',1,'nameContains','vesicle')</tt>
 +
 +
will store in the cell array <tt>files</tt> the names of the models in volume one that include the string 'vesicle' in their filenames (excluding the path).

Latest revision as of 16:09, 23 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 a given series of commands.

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 extract model sets from a catalogue).

% myModels is a cellArray;
% creates a table for each model in a list of membrane models
for i=1:length(myModelFiles)
    m = dread(myModelFiles{i}); % brings the model i into memory
    m.controlUpdate();   % creates the control points 
    m.createMesh();   % creates the depiction mesh
    m.refineMesh();  % refines the depiction mesh
    m.crop_mesh_parameter = 5;
    m.createCropMesh(); % creates cropping points
    m.updateCrop();    % creates the table associated to the model
    m.saveInCatalogue();   % brings the updated model back into the catalogue in disk
end 

Safe execution of loops on cell arrays

Sometimes a given operation can't be executed on all members of a cell array. The same operation may fail on a given model: maybe it doesn't contain enough points, the points are not correctly defined, or any other reason. In such cases, it might be frustrating to see the loop failing for some value of the counter i, thus driving the loop to skip execution on later values of the loop.

Thus, in some cases it is convenient to write loops defensively, i.e., letting the loop carry on its execution even if some step fails. This can be accomplished with a try ... catch block, like in this code:

 % myModels is a cellArray;
o = mbs.o; % just an aid to print on screen.
 for i=1:length(myModelFiles)
    m = dread(myModelFiles{i}); % brings the model i into memory
    try         
          m.controlUpdate();   % creates the control points 
          m. createMesh();   % creates the depiction mesh
          m.refineMesh();  % refines the depiction mesh
          m.crop_mesh_parameter = 5;
          m.createCropMesh(); % creates cropping points
          m.updateCrop();    % creates the table associated to the model 
     catch ME
          o.error();
          o.e('Something happened with model %d',i);
          disp(lasterr);
    end
    m.saveInCatalogue();   % brings the updated model back into the catalogue in disk

     % prints a summary of what happened
     o.e('Model %d has now %d crop_points',{i,size(m.crop_points,1)});
end 

Extracting model files from a catalogue

Through dcm

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.

Through dcmodels

The dynamo_catalogue_models offers a simplified syntax and some more options for retrieval of model files:

files = dcmodels('testCatalogue','i',1,'nameContains','vesicle')

will store in the cell array files the names of the models in volume one that include the string 'vesicle' in their filenames (excluding the path).