Re-sizing structure arrays
Mostrar comentarios más antiguos
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
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
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
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
el 23 de Mzo. de 2016
@Eric: Telling you, that this is a bad idea is more helpful as you are aware of currently.
"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.
Respuesta aceptada
Más respuestas (3)
Image Analyst
el 23 de Mzo. de 2016
0 votos
You don't want to do that. See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
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
el 23 de Mzo. de 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 comentario
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...
Categorías
Más información sobre Structures en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!