For loop, Brace indexing is not supported for variables of this type.

23 views (last 30 days)
This is my for loop, I wasnt too sure how set up the for loop
All_Values = [ ElecsB; ElecsU; EarsB; EarsU; FaceB; FaceU; NeckB; NeckU];
for i = 1: length(All_Values)
a(i,:) = length(find(All_Values{i}(:,4)>25))/length(All_Values{i}(:,4));
end
I am getting this error message. Can you please point me how to fix this error?
Brace indexing is not supported for variables of this type.
Error in Allmodels (line 18)
a(i,:) = length(find(All_Values{i}(:,4)>25))/length(All_Values{i}(:,4));

Accepted Answer

Star Strider
Star Strider on 29 Apr 2022
The ‘All_Values’ matrix is not a cell array, so you cannot use cell array indexing for it. (I am not exactly certain what it contains.)
If it is defined as:
[ ElecsB, ElecsU, EarsB, EarsU, FaceB, FaceU, NeckB, NeckU] = deal(rand(1,10)*35); % Create Unsupplied Variables
All_Values = { ElecsB; ElecsU; EarsB; EarsU; FaceB; FaceU; NeckB; NeckU }; % Define As Cell Array
for i = 1: length(All_Values)
a(i,:) = length(find(All_Values{i}(:,4)>25))/length(All_Values{i}(:,4));
end
a = 8×1
0 0 0 0 0 0 0 0
With that change, it works.
.
  4 Comments
Image Analyst
Image Analyst on 29 Apr 2022
OK, great, but you still might want to review the link I gave below. There is lots of other good stuff in the FAQ too besides that.
Star Strider
Star Strider on 29 Apr 2022
Note that length is a bit ambiguous with respect to cell arrays.
[ ElecsB, ElecsU, EarsB, EarsU, FaceB, FaceU, NeckB, NeckU] = deal(mat2cell(rand(10,32)*35, ones(1,10), 4*ones(1,8))); % Create Unsupplied Variables
All_Values = { ElecsB; ElecsU; EarsB; EarsU; FaceB; FaceU; NeckB; NeckU } % Define As Cell Array
All_Values = 8×1 cell array
{10×8 cell} {10×8 cell} {10×8 cell} {10×8 cell} {10×8 cell} {10×8 cell} {10×8 cell} {10×8 cell}
[rows,cols] = cellfun(@size, All_Values)
rows = 8×1
10 10 10 10 10 10 10 10
cols = 8×1
8 8 8 8 8 8 8 8
for i = 1: length(All_Values)
La = cellfun(@gt,All_Values{i}(:,4), repelem({25},rows(1),1), 'Unif',0) % Logical Array
a(i,:) = length(find(All_Values{i}(:,4)>25))/length(All_Values{i}(:,4));
end
La = 10×1 cell array
{[0 0 0 0]} {[0 0 1 0]} {[0 0 0 0]} {[1 0 0 0]} {[1 0 0 0]} {[0 0 0 0]} {[0 0 1 1]} {[0 1 0 0]} {[0 1 1 1]} {[0 1 1 0]}
Operator '>' is not supported for operands of type 'cell'.
I managed to get the comparison working and producing the ‘La’ logical array. I leave the rest to you, since I’m not certain what you want to do.
The error refers to the ‘a(:,i)’ assignment. That is solved in ‘La’ and since I do not understand what the ‘a(i,:)’ assignment does so I defer to you to implement it.
.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 29 Apr 2022
All_Values is not a cell array. It is a normal double array (probably) so you should use parentheses.
See the FAQ for a description of cell arrays and when to use braces, parentheses, and brackets:

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!