The module "vars2struct" of the Mastrave modelling library


Daniele de Rigo


Copyright and license notice of the function vars2struct



Copyright © 2009,2010,2011 Daniele de Rigo

The file vars2struct.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

Function declaration



[var_struct, var_info] = vars2struct( context = 'caller' ,
                                      pattern = '.*'     )




Utility for collecting in a single structure var_struct the variables of 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 variable can be selected by passing a regular expression as optional input argument pattern . As optional second output argument, a structure var_info containing for each varible the same information provided by @whos can be returned.

Input arguments



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

 pattern            ::regexp::
                    Regular expression to be used for selecting
                    a subset of variable to be copied from the 
                     context  workspace to the structure  var_struct .
                    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' )
   % 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   
   var_struct = vars2struct

   % Passing the context from which variables have to be copied
   test_fun   = @(context) vars2struct( context )
   test_fun( 'caller' )
   test_fun( 'base' )

   % Passing a regular expression to limit the selection of
   % variables to be copied
   var_struct = vars2struct( 'caller', 'a' )
   var_struct = vars2struct( 'caller', '[fz]' )

   % Obtaining information on the selected variables
   [var_struct, var_info] = vars2struct( 'caller', '[fz]' )
   assert( isequal( fieldnames(var_struct), {var_info(:).name}.' ) )

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

See also:

   workspace, memory, variables, struct

Version: 0.4.9




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.

Valid XHTML 1.0 Transitional