20421

Do I always need to use a cell array to assign multiple values to a struct array?

I've got a nested struct array like

A(1).B(1).var1 = 1; A(1).B(2).var1 = 2;

Now I want to change the values of var1 to using the elements of the vector x = [3; 4] for each of the respective values.

The result should be

A(1).B(1).var1 = 3; A(1).B(2).var1 = 4;

I have tried

% Error : Scalar structure required for this assignment. A(1).B.var1 = x; % Error : Insufficient number of outputs from right hand side of equal sign to satisfy assignment. [A(1).B.var1] = x(:);

Curiously, if x is a cell array, the second syntax works

x = {3, 4}; [A(1).B.var1] = x{:};

Luckily, it's not too complicated to convert my numeric vector to a cell array using mat2cell, but is that the only way to do this assignment without a for loop?

What's the correct syntax for multiple assignment to a nested struct array? Can I use numeric vectors or do I have to use cell arrays?

Answer1:

The statement

[A(1).B.var1] = x{:};

is shorthand for

[A(1).B.var1] = deal(x{:});

(see the documentation for deal).

Thus you can also write

[A(1).B.var1] = deal(3,4);

I'm not aware of any other way to assign different values to a field in a struct array in a single command.

If your values are in a numeric array, you can easily convert it to a cell array using num2cell (which is simpler than the mat2cell you found).

data = [3,4]; tmp = num2cell(data); [A(1).B.var1] = tmp{:};

In general, struct arrays are rather awkward to use for cases like this. If you can, I would recommend that you store your data in normal numeric arrays, which make it easier to manipulate many elements at the same time. If you insist on using a struct array (which is convenient for certain situations), simply use a for loop:

data = [3,4]; for ii = 1:length(A(1).B) A(1).B(ii).var1 = data(ii); end

The other alternative is to use table.

Recommend

  • MATLAB using MAT2CELL
  • Split a matrix into smaller matrices
  • Why generic T is inferred as a literal type only when it has a constraint?
  • Get raw packet data from Qt application
  • How can I use indexing on the output of a function? [duplicate]
  • Galaxy Tablet browser sniff / detection
  • Problem with assigning elements of a class array to individual variables in MATLAB
  • UML specification : Do I need inheritance?
  • .Net Compact Framework 3.5 HTTP POST
  • Int to char conversion rule in C when int is outside the range of char
  • how to insert a new column in a dataset with values if it satisfies a statement
  • Play Framework nested form errors missing
  • Download/Save/Write a file on the client's hard disk using flash/flex
  • reduce/reduce conflicts using ocamlyacc
  • CSS: How to fix overlapping divs
  • How to write string.Contains(someText) in expression Tree
  • How to get the index of element in the List in c#
  • Calling java project from Mathematica
  • PHP file_exists() anomaly
  • Rails AREL .where statement
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Add reference to ASP.NET 5 Class Library from Framework 4.5 Class Library Project
  • How can I run DataNucleus Bytecode Enhancer from SBT?
  • Jquery Knockout: ko.computed() vs classic function?
  • Center align outputs in ipython notebook
  • Ember.js model to be organised as a tree structure
  • Ajax calls do not work in IE unless you fiddle with security settings
  • Inline R code in YAML for rmarkdown doesn't run
  • custom UITableViewCell with image for highlighting
  • Jackson Parser: ignore deserializing for type mismatch
  • How to define and use opencv mat of user type
  • OpenGL ES texture problem, 4 duplicate columns and horizontal lines (Android)
  • Is there a javascript serializer for JSON.Net?
  • Azure Cloud Service Web Role web pages do not load
  • Sending data from AppleScript to FileMaker records
  • Where to put my custom functions in Wordpress?
  • vba code to select only visible cells in specific column except heading
  • How can I estimate amount of memory left with calling System.gc()?
  • Buffer size for converting unsigned long to string
  • Binding checkboxes to object values in AngularJs