Unable to perform assignment because brace indexing is not supported for variables of this type.

Dear Matlab Community,
in a loop I am trying to write variables into a cell.
For variable sub_matrix it works, however for sub_matrix_idx it does not.
The error message:Unable to perform assignment because brace indexing is not supported for variables of this type.
The former are pixel values of an image in double format, the latter returns the position of these pixels on the image (also double, please see attached).
Any suggestions are appreciated
best
lg

5 Comments

We need to see a demonstration of what you did, which you can provide using the Run button
Dear MAtt J please find the code with the working indexing only.
clear all
I=imread ('pout.tif');
I=im2double(I);
imshow(I);
h = drawfreehand;
bw = createMask(h);
I(bw==1)=NaN;
[r,c] = size(I);
sz=[r c];
roi_x=9;
roi_y=9;
index=1;
sub_matrices=cell(1, 10);
for i=1:1000
startpoint=(randi(numel(I)));
[row,col] = ind2sub(sz,startpoint);
if col+roi_x>c
col=col-(roi_x+1);
end
if row+roi_y>r
row=row-(roi_y+1);
end
sub_matrix = I(row:(row+roi_y),col:(col+roi_x));
I_idx=1:numel(I);
I_idx=reshape (I_idx,[291,240]);
sub_matrix_idx=I_idx(row:(row+roi_y),col:(col+roi_x));
if anynan(sub_matrix)==1
index = index;
continue
else
index=index+1;
end
roi = images.roi.Rectangle(gca,'Position',[col,row,roi_x,roi_y]);
if index == 11
sub_matrices{1, index-1}=sub_matrix;
break
end
sub_matrices{1, index-1}=sub_matrix;
end
Dear Walter Robertson,
if I try to write sub_matrix_idx to a cell like I do witH "sub_matrices{1, index-1}=sub_matrix;"
I got the error message.
thanks
lg
What does this show
whos sub_matrix
whos sub_matrices
I ran your code, drew a region, and it finished with no error. I tried several time.
But what are you trying to do? In words, rather than code. There might be a better way.
I attach some demos that might help.

Sign in to comment.

 Accepted Answer

Your posted code has no indexed assignments to sub_matrix_idx so you need to post the code that you actually used.
I_idx=1:numel(I);
Numeric.
I_idx=reshape (I_idx,[291,240]);
reshape() of a numeric is numeric.
sub_matrix_idx=I_idx(row:(row+roi_y),col:(col+roi_x));
() indexing of something numeric gives a numeric result.
So sub_matrix_idx is numeric, not cell. Using {} indexing to try to assign into it would be a mistake.
You could convert sub_matrix_idx into a cell using
sub_matrix_idx = num2cell(sub_matrix_idx);
but if you do, then watch out for your
if anynan(sub_matrix)==1
statement, as anynan() cannot operate on cell arrays.

More Answers (2)

It looks like sub_matrix is a matrix rather than a cell array, so it should be accessed with parenthesis:
sub_matrix(i, j) = ...
and not with braces:
sub_matrix{i, j} = ...

Community Treasure Hunt

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

Start Hunting!