The module "struct2vars" of the Mastrave modelling library

 

Daniele de Rigo

 


Copyright and license notice of the function struct2vars

 

 

Copyright © 2009,2010,2011 Daniele de Rigo

The file struct2vars.m is part of Mastrave.

Mastrave is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Mastrave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with Mastrave. If not, see http://www.gnu.org/licenses/.

Function declaration

 

 

var_info = struct2vars( var_struct            ,
                        context    = 'caller' ,
                        pattern    = '.*'     )

Description

 

 

Utility for expanding the fields of a structure var_struct into a corresponding set of varibles to be exported in the workspace memory of a given context (which can be either the base workspace or the one of the caller which is invoking the utility - this latter context is used by default). A subset of all available fields can be selected by passing a regular expression as optional input argument pattern . The structure var_info containing for each varible the same information provided by @whos can be returned.

Input arguments

 

 


 var_struct         ::struct::
                    Structure whose fields have to be exported as 
                    variables within the memory of the workspace
                     context .

 context            ::string::
                    Context indicating the workspace from which the
                    fields in  var_struct  have to be copied into the 
                    corresponding variables in the workspace  context .  
                    If omitted, its default value is 'caller'.
                    Valid contexts are:
                    
                          context        meaning
                       ────────────────────────────────────────────────
                          'caller'     Copy to the workspace memory
                                       of the caller which is invoking
                                       the utility.
                       ───────────────────────────────────────────────
                          'base'       Copy to the base workspace
                                       memory.

 pattern            ::regexp::
                    Regular expression to be used for selecting
                    a subset of  var_struct  fields to be copied to the 
                     context  workspace.
                    If omitted, its default value is the always matching
                    regular expression '.*' .


Example of usage

 

 


   % Basic usage
   % Saving the current workspace to enable it to be subsequently
   % resored
   save( '/tmp/current_workspace.m' )
   clear
   % Assinging some variables 
   foo  = 1:10
   bar  = 'one two three ...'
   baz  = { pi, 'ratio of the circumference of a circle to its diameter' }
   % List of the current varibles   
   who
   var_struct = vars2struct

   % Removing some variables
   clear( 'foo', 'bar', 'baz' )

   % Restoring them from  var_struct
   struct2vars( var_struct )
   foo, bar, baz

   % Passing a regular expression to limit the selection of
   % variables to be copied
   clear( 'foo', 'bar', 'baz' )
   struct2vars( var_struct, 'caller', 'a' )
   whos

   clear( 'foo', 'bar', 'baz' )
   var_info = struct2vars( var_struct, 'caller', '[fz]' );
   whos

   % Obtaining information on the selected variables
   for i=1:numel(var_info) disp( var_info(i) ); end

   % Restoring the original variables of the workspace
   clear all
   load( '/tmp/current_workspace.m' )


See also:
   vars2struct



Keywords:
   workspace, memory, variables, struct



Version: 0.4.8

Support

 

 

The Mastrave modelling library is committed to provide reusable and general - but also robust and scalable - modules for research modellers dealing with computational science.  You can help the Mastrave project by providing feedbacks on unexpected behaviours of this module.  Despite all efforts, all of us - either developers or users - (should) know that errors are unavoidable.  However, the free software paradigm successfully highlights that scientific knowledge freedom also implies an impressive opportunity for collectively evolve the tools and ideas upon which our daily work is based.  Reporting a problem that you found using Mastrave may help the developer team to find a possible bug.  Please, be aware that Mastrave is entirely based on voluntary efforts: in order for your help to be as effective as possible, please read carefully the section on reporting problems.  Thank you for your collaboration.

Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Daniele de Rigo

This page is licensed under a Creative Commons Attribution-NoDerivs 3.0 Italy License.

This document is also part of the book:
de Rigo, D. (2012). Semantic Array Programming with Mastrave - Introduction to Semantic Computational Modelling. http://mastrave.org/doc/MTV-1.012-1


Valid XHTML 1.0 Transitional