Re-sizing structure arrays

12 visualizzazioni (ultimi 30 giorni)
Eric Lopresti
Eric Lopresti il 23 Mar 2016
Modificato: Stephen23 il 19 Giu 2019
How can I make a structure array of unknown size into several 1x1 structures? For instance, changing a 5x1 structure array into 5 1x1 structures.
EDIT: Telling me it's a bad idea does not answer my question.
EDIT2: I apologize for my earlier comment about helpfulness, I was just frustrated. What I'm trying to do is take a structure array (which obviously only states its dimensions and specifies the fieldnames when displayed) and display it as a character array containing the entirety of the data. Now I can convert a 1x1 to a character array just fine because I found a .p file for that, but I'm having problems breaking my structures into pieces that script can handle.
  5 Commenti
Jan
Jan il 23 Mar 2016
@Eric: Telling you, that this is a bad idea is more helpful as you are aware of currently.
Stephen23
Stephen23 il 23 Mar 2016
Modificato: Stephen23 il 19 Giu 2019
"Please either be helpful"
We were more helpful than you realize.
If you tell us actually what you are trying to achieve than we can show you a fast, neat, and efficient way of doing it without requiring lots of separate variables. So far you have not told us anything about what you intend to do with these structures. If you tell us what you are doing we can show you a better solution without requiring ugly dynamic variable accessing.

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 24 Mar 2016
Eric, you can use fprintf() to display any fields you want of any or all structures in a structure array. For example:
for k = 1 : length(s)
fprintf('s(%d).str1 = %s\n', k, s(k).str1); % Display a string field
fprintf('s(%d).int1 = %d\n', k, s(k).int1); % Display a integer numerical field
fprintf('s(%d).dbl = %.2f\n', k, s(k).dbl); % Display a double numerical field
end
This will print those 3 fields to the command window for all structures in the array of structures.
  2 Commenti
Walter Roberson
Walter Roberson il 24 Mar 2016
Modificato: Walter Roberson il 24 Mar 2016
And of course you can
for k = 1 : length(s); disp(s(k)); end
Or
arrayfun(@(IDX) evalc(disp(s(IDX)), (1:length(s)).', 'Uniform', 0)
Eric Lopresti
Eric Lopresti il 24 Mar 2016
Still not exactly what I was asking, but it works for what I needed. Thanks to all for the help!

Accedi per commentare.

Più risposte (3)

Image Analyst
Image Analyst il 23 Mar 2016
After all, if you didn't know how many structures there were in the structure array, then how would you use the individual structures later if you didn't know there names? For example if str had 9 structures, then you made str1, str2, str3, ...str9, then how would you use str9 later in your code? What if you tried to, say thisStruct = str9, but str9 did not exist because this time you had only 5 structures? It's much better to leave it as an array of structures.

Stephen23
Stephen23 il 23 Mar 2016
Modificato: Stephen23 il 19 Giu 2019

Jan
Jan il 23 Mar 2016
I agree with rejecting the idea to create a set of variables dynamically.
But perhaps you are satisfied with a cell array of structs? Then you can add specific fields to some of the structs only, while in a struct array added fields appear in all structs.
a(1).field1 = 1
a(2).field2 = 2 % This creates a(1).field2=[] automatically
c = cell(size(a));
for k = 1:numel(a)
c{k} = a(k);
end
c{2}.field3 = '3'; % This does not modify the struct c{1}
This is no magic creation of dynamic variables and does not have the evil effects of eval. The data are still structured and easy to debug, but you have a set of structs instead of a struct array.
  1 Commento
Stephen23
Stephen23 il 23 Mar 2016
It seems strange to advise going from simple data (one non-scalar structure) to a more complicated arrangement. I am not criticizing your solution, but pointing out that if someone were to use this they would be choosing to use a more complicated data arrangement (slower, larger, difficult to access) than simply using the non-scalar structure that they already have...

Accedi per commentare.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by