How to select more than one cells in a structure?

Hello everybody, I have an 1x294 struct with 25 fields but I need to create a variable type double with only a specific range of cells. I mean, if i write a=struct(1).d Matlab take from the array the position 1 and create a doble variable with the information of 1. But I need to crate a doble variable for example with the cells 4...23. How do I do that?
thank you

 Accepted Answer

Hello Juan,
I think I understand what you're asking. You're looking to extract a double value from one field in each element in a struct array. Something like the equivalent of this:
s = struct('a',1);
s(2).a = 2
out(1) = s(1).a;
out(2) = s(2).a;
...
You can use the handy comma-separated lists property of MATLAB to do this in a couple lines:
out = cell(size(s));
[out{:}] = s(:).a;
And if you want a double array rather than a cell array:
outDouble = cell2mat(out);
-Cam

4 Comments

Hello Cam, thank you for your support. I am trying to do something like this:
a=1x2294 struct
out=a(1).d
to extract the information of the position 1 of the structure, but I need to extract the information from more than one cells to make a double array with that information.
thank you so much
I'm probably getting confused by your use of the word "cells". Is there a cell array involved at all? What is the datatype of the value contained in a(1).d?
If by "cells" you mean "elements of the struct array", then my answer previously shows you how to do that. Was there an issue with that? Can you make an example (1x3 struct) that shows what you've tried and what the issue is?
this is a file. I need to use all the information from each fields. If I do a(1).d I get a variable of the type double.
Alright, your original question was that you wanted a double array with the information from "cells 4 through 23". Does that mean you want some kind of data structure with information from:
a(1).('StationIdentifierCode', 'LocationIdentifier', etc.)
or you wanted:
a(4:23).d
In other words, do you want the information from one "row" in that image you posted, or one "column"?
I use "row" and "column" in quotes because it's not really a matrix. That's just how it is displayed for the variable editor. Also note that there is a lot of non-numeric data in different fields.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!