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 comentarios

Stephen23
Stephen23 el 23 de Mzo. de 2016
Editada: Stephen23 el 23 de Mzo. de 2016
Don't.
Whatever data processing you need to do will be a thousand times faster and more robust when you use one 5x1 structure rather than writing slow hack code to create and access lots of separate scalar structures. Why change the data into something that is much harder to process??
The documentation explains clearly how to access the data in a non-scalar structure: practice and you will find that it is pretty easy:
Image Analyst
Image Analyst el 23 de Mzo. de 2016
Eric, your edit is not applicable. We did both: we told you that it was a bad idea, and showed you how to find out how to do this bad idea if, against all advice, you decide to go ahead with the bad idea.
Because you said we didn't tell you how to do it clearly indicates that you did not follow the link I gave you. If you had you would have seen how to do this unwise thing, so I'm not going to repeat the code here because we don't like it, and it's there in that link already. You're free to do as you wish though.
Walter Roberson
Walter Roberson el 23 de Mzo. de 2016
Eric, you are free to ask elsewhere like StackOverflow if you do not want to be informed of the tradeoffs involved in the solutions. This is an educational resource; we teach not only commands but also when not to use the commands.
Jan
Jan el 23 de Mzo. de 2016
@Eric: Telling you, that this is a bad idea is more helpful as you are aware of currently.
Stephen23
Stephen23 el 23 de Mzo. de 2016
Editada: Stephen23 el 19 de Jun. de 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.

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 24 de Mzo. de 2016

1 voto

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 comentarios

Walter Roberson
Walter Roberson el 24 de Mzo. de 2016
Editada: Walter Roberson el 24 de Mzo. de 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 el 24 de Mzo. de 2016
Still not exactly what I was asking, but it works for what I needed. Thanks to all for the help!

Iniciar sesión para comentar.

Más respuestas (3)

Image Analyst
Image Analyst el 23 de Mzo. de 2016

0 votos

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.
Jan
Jan el 23 de Mzo. de 2016

0 votos

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 comentario

Stephen23
Stephen23 el 23 de Mzo. de 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...

Iniciar sesión para comentar.

Categorías

Preguntada:

el 23 de Mzo. de 2016

Editada:

el 19 de Jun. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by