Creating an array of structs and using the field directly?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Jan Kappen
am 19 Nov. 2015
Kommentiert: Jan
am 23 Nov. 2015
Hey guys. I'm wondering if there is a workaround for doing something like this:
a = {[struct('field',1) struct('field',2)].field}
which works in octave but not in matlab ("invalid syntax at '.'. Possibly a ')', ']' or '}' is missing"), I have to use a temporary variable. This is quite annoying. Is there a workaround?
Thanks!
0 Kommentare
Akzeptierte Antwort
Stephen23
am 19 Nov. 2015
Bearbeitet: Stephen23
am 19 Nov. 2015
It is not clear why you need to structure at all, as the output is simply a cell array with some numeric scalars in it (Octave code below)
>> {[struct('field',1) struct('field',2)].field}
ans =
{
[1,1] = 1
[1,2] = 2
}
>> {struct('field',{1,2}).field}
ans =
{
[1,1] = 1
[1,2] = 2
}
>> {1,2}
ans =
{
[1,1] = 1
[1,2] = 2
}
If the input is already a structure, then you can simply access the field values using the methods given in the documentation for non-scalar structures:
{X.field}
6 Kommentare
Weitere Antworten (1)
Jan
am 19 Nov. 2015
As long as you insist on using standard struct : No, there is no workaround. Using a temporary variable is not a big deal and does not waste additional resources.
You can create your own object class, which solves the problem. But this will be less efficient.
3 Kommentare
Jan
am 23 Nov. 2015
You can accept one answer only. But you can vote for more answers and a comment is useful also.
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!