struct to cell array
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Steve Miller
am 18 Jan. 2019
Bearbeitet: Stephen23
am 19 Jan. 2019
I was working with structs and cell array and I came across something I didn't quite understand.
If I have a struct and its transpose -
s = struct('A',{1;2;3}) % 3x1 struct
sT = struct('A',{1,2,3}) % 1x3 struct
Then using dot notation to select a single field will produce a cell array with the same dimensions either way -
d = {s.A} % 1x3 cell
dT = {sT.A} % 1x3 cell
Why aren't d and dT transposes of each other?
1 Kommentar
Stephen23
am 19 Jan. 2019
Bearbeitet: Stephen23
am 19 Jan. 2019
"Why aren't d and dT transposes of each other?"
Comma-separated lists are a powerful and useful feature, once their concept is understood.
The reason why is because the comma-separated list that you generated is not one variable as you seem to think (which can have a size or orientation) but is simply a list of scalar variables, and such a list does not have anything like a size or orientation. It is just a list, separated by commas. Both of your examples produce exactly the same comma-separated list:
1,2,3
and then call the cell array constructor with that (same) comma-separated list:
{1,2,3}
The size of your output (if any) depends entirely on the function that you are entering that comma-separated list into as input arguments (i.e. the cell array constructor). Of course with matrices/arrays the order of the comma-separated list can be different, depending on the orientation of the array, but for vectors their orientation makes absolutely no difference.
So, simply put, a comma-separated list does not have size, therefore the output size depends only on the function that you are using that comma-separated list with.
Read these to know more:
Akzeptierte Antwort
per isakson
am 18 Jan. 2019
Bearbeitet: per isakson
am 18 Jan. 2019
Why? Because Matlab works that way.
>> nums.f
ans =
1
ans =
2
ans =
3
outputs a list and
{}
concatenates the list horisontally into a cell array.
0 Kommentare
Weitere Antworten (1)
Walter Roberson
am 18 Jan. 2019
What you are doing is structure expansion, which is comma separated lists. So you are getting
{sT(1).A, sT(2).A, sT(3).A}
0 Kommentare
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!