By Doug Hull
12:28 UTC | Posted in Format: PodCast, Format: Video, Level: Basic | Permalink |
You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.
It’s easier to create vector s by: s = [people.SimmNum];
This and Emil’s comment were quite helpful to me.
To take this one step further, one advantage of Arrays of Structures would be the ability to have vectors of different lengths for each numerical part, and a cell array of names of different lengths for each name part.
For example:
people(1).name=’Erin’ people(2).name={’Joan’ ‘Carla’} people(1).sim=44 people(2).sim=[561 49]
Emil,
Yes, you are correct. I can not believe I over looked that. There are so many ways to do certain things in MATLAB.
Doug
If you have an array that you want to assign to a field across a structure array, you can do this:
[structArray(:).fieldname] = deal(num2cell(array))
Of course, the length of array should be the same as the length of structArray, but if structArray doesn’t exist yet, then you can do:
[structArray(1:length(array)).fieldname] = deal(num2cell(array))
If array is already a cell array, you can avoid using both deal and num2cell (although they will still work) and use:
[structArray(:).fieldname] = array{:} or [structArray(1:length(array)).fieldname] = array{:}
An example, modifying the struct array ‘people’ from the video:
names = {’Larry’,'Moe’,'Curly’}; [people(:).name] = names{:};
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 "<" (including the semicolon).
Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.
These postings are the author's and don't necessarily represent the opinions of The MathWorks.
It’s easier to create vector s by:
s = [people.SimmNum];
This and Emil’s comment were quite helpful to me.
To take this one step further, one advantage of Arrays of Structures would be the ability to have vectors of different lengths for each numerical part, and a cell array of names of different lengths for each name part.
For example:
people(1).name=’Erin’
people(2).name={’Joan’ ‘Carla’}
people(1).sim=44
people(2).sim=[561 49]
Emil,
Yes, you are correct. I can not believe I over looked that. There are so many ways to do certain things in MATLAB.
Doug
If you have an array that you want to assign to a field across a structure array, you can do this:
[structArray(:).fieldname] = deal(num2cell(array))
Of course, the length of array should be the same as the length of structArray, but if structArray doesn’t exist yet, then you can do:
[structArray(1:length(array)).fieldname] = deal(num2cell(array))
If array is already a cell array, you can avoid using both deal and num2cell (although they will still work) and use:
[structArray(:).fieldname] = array{:}
or
[structArray(1:length(array)).fieldname] = array{:}
An example, modifying the struct array ‘people’ from the video:
names = {’Larry’,'Moe’,'Curly’};
[people(:).name] = names{:};