For Loops with Struct and Fields

조회 수: 2 (최근 30일)
Franc
Franc 2019년 2월 17일
댓글: Franc 2019년 2월 18일
My data is in a structure/filed format B{x}.F{x}.signals.values, The structures and fields are consistant ,Struct B{x} where x =1:inf, F{x} and signals are the same for every iteration of B{x}. However the values are of diffrent sizes and i need to vertically cancatenate to be able to plot the values in on graph.
I have been using the loop function below to try to get te data into one cell C so I use the Padcat function but either 6 or 8 produced errors
C ={};
cj =1;
j =1;
for j = 3:numel(B)
S = B{1,j};
fn = fieldnames(B{1,j}).';
for y = fn{1,j}
C{cj} = S.(y{:}).signals.values;
cj = cj+1;
end
end
Is there a way to better concantenate large cell or why do i get "Brace indexing is not supported for
variables of this type"
Thanks.
  댓글 수: 2
Stephen23
Stephen23 2019년 2월 18일
편집: Stephen23 2019년 2월 18일
'...why do i get "Brace indexing is not supported for variables of this type" '
fieldnames returns a cell array of char vectors, so that is what fn is. Then with
for y = fn{1,j}
you take out the contents of the j-th cell, which of course is a character vector. You use this character vector to define the for loop, i.e. you told MATLAB that you want to iterate over the elements of a character vector. Each element of a character vector is one character (i.e. y will be each character of that vector). Inside the loop you try to use curly brace indexing on that character that you defined the loop iterator y to be:
y{:}
Curly brace indexing is defined for cell arrays, tables, and strings, but not for character arrays (or numeric arrays or logical arrays). Thus the error.
Instead of getting confused by simple errors, a good place to start is actually looking at the variables themselves: many errors that beginners ask us about can be fixed by looking at the variables and seeing that they have incompatible sizes/types/indexing/...
Franc
Franc 2019년 2월 18일
thanks

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by