File Exchange Pick of the Week

August 5th, 2011

Converting variables to structures, and vice versa

Jiro's pick this week is v2struct by Adi Navve.

I have a special liking for convenience/utility functions, because they make my life easier. For example, here's something that many users have probably learned to do early on:

  plot(x, y, 'ro:')

Did you know that plot is a high-level, convenience function for a much more flexible, low-level function line?

  line(x, y, 'Color', 'r', 'Marker', 'o', 'LineStyle', ':')

Adi's v2struct is a function that makes converting between structures and individual variables easier.

Variables to a Structure

Suppose you have a bunch of variables that you want to combine into a single structure:

aString = 'August 5, 2011';
aScalar = pi;
aVector = rand(1, 5);

v2struct automatically uses the names of the variables as the field names:

s = v2struct(aString, aScalar, aVector)
s = 
    aString: 'August 5, 2011'
    aScalar: 3.1416
    aVector: [0.8407 0.2543 0.8143 0.2435 0.9293]

Structure to Variables

The function works the other way also. Suppose you have a structure like this:

s2 = struct('var1', 1, 'var2', [1 2 3], 'var3', 'hello')
s2 = 
    var1: 1
    var2: [1 2 3]
    var3: 'hello'

You can create variables from the fields of the structure:

v2struct(s2)
whos var*
  Name      Size            Bytes  Class     Attributes

  var1      1x1                 8  double              
  var2      1x3                24  double              
  var3      1x5                10  char                

In addition to these basic uses, v2struct provides advanced functionalities, such as

  • creating structures with field names different from variable names
  • updating fields of an existing structure
  • extracting only specific fields of a structure
  • extracting fields with a different variable name

What I also like about Adi's function is that there is a very extensive help section with lots of examples. I can see he has put in a lot of effort in making this easy to use.

Comments

Let us know what you think here or leave a comment for Adi.


Get the MATLAB code

Published with MATLAB® 7.12

2 Responses to “Converting variables to structures, and vice versa”

  1. Tommy M replied on :

    I have been looking for something like this for a long time. I’ve been trying to tackle the problem of getting my workspace structure variables into a Simulink model for xPC. The problem I have is that the Matlab Embedded Function block does not support passing a structure variable from the workspace as an input. This seems to be a step forward to solving that problem, but with just copy/paste of this function into a Matlab Function for Simulink doesn’t work. There are errors about code generation is only supports cell operations for varargin and varargout. Any ideas?

  2. jiro replied on :

    Hi Tommy,

    I asked around a bit and here’s some general information that may be useful, in case you weren’t aware:

    1) Simulink signals cannot be of class structure. Thus if you’re supplying a signal from Simulink into your MATLAB Function block, then yes, this won’t work.
    2) Simulink signals can be driven from fields of structures if those fields are of a valid class. As an example, I could drive a signal with a constant block whose value is “mystruct.field1″ and feed that into a MATLAB Function.
    3) The analog to a structure within Simulink is a bus object. Bus objects can be created from structure variables. See this page.
    4) Items 2 and 3 are contingent that your fields are of valid Simulink types. Cell arrays and chars cannot be used by Simulink.

    Drag a Constant block into Simulink, go to its Signal Attribute tab, and take a look at the options in the “Output data type” pulldown menu.

    If you are having further problems, I would suggest contacting Support.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


MathWorks

Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

These postings are the author's and don't necessarily represent the opinions of The MathWorks.