Dot indexing error for naming variable
이전 댓글 표시
Hi, I have a loop for processing images using the following code;
path='C:\Users\Udi\Desktop\3rd Year Project\2. GMM\new folder';
I = dir(fullfile(path, '*.jpg'));
for k = 1:numel(I)
filename = fullfile(path, I(k).name);
I2 = imread(filename);
b = I2(:,:,1);
d.(k)= uint8(b) .* uint8(b>180);
%figure, imshow(d)
e.(filename)=d{k}(d{k}>0);
save([filename, 'threshold', '.mat'], 'd.(k)')
end
I am saving every output of d and would like the variable to be named as d_k (where k is the number of iteratoin)
I keep getting this error though,
Unable to perform assignment because dot indexing is not supported for variables of this type.
Any help would be appreciated!
답변 (1개)
Walter Roberson
2019년 12월 9일
Either variable d or e exists already and is not a structure.
d.(k)= uint8(b) .* uint8(b>180);
The above line is a problem: k is a number, and using a number as a dynamic field name is not supported. You might want d{k} as suggested by your later use of d{k}
e.(filename)=d{k}(d{k}>0);
You constructed filename from an entire path including directory. It is likely to be too long for a field name, and it will include path separators which are not valid for field names. You would probably be better off storing the filename in a data structure and using a numeric index for what you are storing separately.
save([filename, 'threshold', '.mat'], 'd.(k)')
filename ends in .jpg so you would be constructing a new filename that might look like
C:\Users\Udi\Desktop\3rd Year Project\2. GMM\new folder\rhinos.jpgthreshold.mat
which is unlikely to be what you want.
It is also not possible to save() part of a variable, only an entire variable. You can save all of d but to save the current d{k} you would have to assign that to a variable and save the variable.
e.(filename)=d{k}(d{k}>0);
The right hand side of that is probably valid, but be warned that it produces a vector result in which you cannot tell where the values came from. Is that what is desired?
b = I2(:,:,1);
It is a bit confusing to name the red channel b
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!