How to use the fields in the structure?

My structure has 83 Fields, and each field has 1059 *3 Double. Firstly how do I access these fields from the structure and then how do I multiply a matrix(rotation matrix) on these fields?. I know that I have to use for loop to access all the values and I have found this code But I do not know how It does function.
for i = fieldnames(markerStruct)'
newMarkerStruct.(i{1}) = markerStruct.(i{1});
end
I understand that values from markerStruct gets copied to newmarkerStruct. But I don't understand what i{1} means and How the value is getting assigned.

 Respuesta aceptada

Adam Danz
Adam Danz el 28 de Feb. de 2019
Editada: Adam Danz el 28 de Feb. de 2019
I've responded to your quesitons below. First I make some fake data to work with.
% Fake data
S.f1 = rand(1059, 3);
S.f2 = rand(1059, 3);
S.f3 = rand(1059, 3);
"Firstly how do I access these fields from the structure?"
% Field f1 is accessed like this
S.f1
% or dynamically
fn = fieldnames(S); %list of field names
S.(fn{1})
% add a field
S.f4 = rand(1059, 3);
"How do I multiply a matrix(rotation matrix) on these fields?"
% rotation matrix
ry = [ cos(pi) 0 sin(pi);
0 1 0 ;
-sin(pi) 0 cos(pi)] ;
% Multiply the matrix by the first field .
f1Rot = S.f1 * ry;
"I know that I have to use for loop to access all the values "
% Actually, not true. You can use structfun() to apply a function to all fields.
% In this example, it's expected that all fields have 3 columns of data.
S2 = structfun(@(x) x * ry, S, 'UniformOutput', false);
% But if you must use a loop
fn = fieldnames(S); %list of field names
S2 = struct; % create new structure (no fields yet)
for i = 1:length(fn)
S2.(fn{i}) = S.(fn{i}) * ry; %store results in a new struct with same field names
end
"I have found this code But I do not know how It does function"
% This for-loop does nothing. It's just looping through each field and re-assigning the identical value.
% It's like A = A.
for i = fieldnames(markerStruct)'
newMarkerStruct.(i{1}) = markerStruct.(i{1});
end
fieldnames() lists all field names in a cell array of strings.

6 comentarios

Thank you for the detailed answer. The last where you explained that it's going through each field and re-assigning the identical value, that I understood. But I did not understand why is that i{1}, What does that 1 signify?
Walter Roberson
Walter Roberson el 28 de Feb. de 2019
fieldnames() returns a cell array of character vectors, in the form of a column vector cell. The ' at the end of the fieldnames(markerStruct) call transforms that to a cell array row vector.
When you for i = something then on each iteration, i will be assigned the next column of the values. With you having a cell array row vector, the columns are cell array scalars -- but still cell arrays. The i{1} is pulling out the content of that cell array scalar, giving you the character vector that is stored inside of it.
The function fieldnames() lists all field names in a cell array. The loop is looping through each field name using dynamic field names which I showed in my examples above.
Here's an example
markerStruct.f1 = 'first';
markerStruct.f2 = 'second';
markerStruct.f3 = 'third';
% list all field names
fn = fieldnames(markerStruct)'
%fn =
% 1×3 cell array
% {'f1'} {'f2'} {'f3'}
% Look at the first field name
fn{1}
%ans =
% 'f1'
% Look at the value stored in the first field
markerStruct.(fn{1})
%ans =
% 'first'
% Loop through all fields
for i = fieldnames(markerStruct)'
markerStruct.(i{1})
end
Walter Roberson
Walter Roberson el 28 de Feb. de 2019
The action of for when given things that are not numeric row vectors is admittedly a bit obscure.
Adam Danz
Adam Danz el 28 de Feb. de 2019
Editada: Adam Danz el 28 de Feb. de 2019
Yeah, to be honest, this is my first time seeing it.
If you're going to use a loop (rather than structfun, which I showed in my examples), then this is the better approach:
fn = fieldnames(S); %list of field names
for i = 1:length(fn)
S.(fn{i}) * ry
end
Thank you so much for the answer. Appreciate it.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements 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!

Translated by