For Loops with Struct and Fields

2 views (last 30 days)
Franc
Franc on 17 Feb 2019
Commented: Franc on 18 Feb 2019
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 Comments
Stephen23
Stephen23 on 18 Feb 2019
Edited: Stephen23 on 18 Feb 2019
'...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/...

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!