Structure Assginment

I need a better approach to assign matrix to a structure. Currently I have
A = 5 by 1 struct with Loc & Err variables, A(1:5).Loc & A(1:5).Err,
I have Values matrix 5 by 6 where Loc info stored.
My code;
for i=1:5
A(i).Loc = Values(i,:)
end
Is there easy way of assignment. I have a very complex structure, this is very simplified version of it. Any ideas??

1 comentario

Daniel Shub
Daniel Shub el 8 de Mayo de 2012
This seems pretty easy to understand for me. What do you mean by easy?

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 8 de Mayo de 2012

0 votos

Try this:
vcell = mat2cell(Values, ones(1, size(Values,1)), size(Values,2));
[A.Loc] = vcell{:};

4 comentarios

Umit
Umit el 8 de Mayo de 2012
It is close but not what I want, Lets say my values are 12 by 6 matrix, this one creates A.Loc 12 by 1, not A by 12 by 1 with each Loc 1 by 6. This is what I want. 'A(12).Loc(6)'
Walter Roberson
Walter Roberson el 8 de Mayo de 2012
I had assumed here that you had already created A as a structure array. Otherwise,
[A(1:size(Values,1)).Loc] = vcell{:};
Daniel Shub
Daniel Shub el 8 de Mayo de 2012
If there are lots of fields you might want to consider cell2struct
Umit
Umit el 8 de Mayo de 2012
It works, thank you. This is what I want

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 8 de Mayo de 2012

1 voto

There is no easy way to assign a very complicated structure.
Using a loop is most likely the easiest and most efficient method, especially if you use implicite pre-allocation by running the loop from the end to the start.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 8 de Mayo de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by