Dot indexing error for naming variable
4 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(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
2 个评论
Stephen23
2019-12-10
"You would probably be better off storing the filename in a data structure..."
It already is: I.
"... and using a numeric index for what you are storing separately"
Easy using I:
I(k).data = ...
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!