For loop, Brace indexing is not supported for variables of this type.
23 views (last 30 days)
Show older comments
Alexander Guillen
on 29 Apr 2022
Commented: Star Strider
on 29 Apr 2022
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));
0 Comments
Accepted Answer
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
With that change, it works.
.
4 Comments
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
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
[rows,cols] = cellfun(@size, All_Values)
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
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.
.
More Answers (1)
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:
0 Comments
See Also
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!