Dot indexing error for naming variable

5 ビュー (過去 30 日間)
udi ibgui
udi ibgui 2019 年 12 月 9 日
コメント済み: udi ibgui 2019 年 12 月 10 日
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
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
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 = ...
udi ibgui
udi ibgui 2019 年 12 月 10 日
Thanks! When I use curly brackets though {}, I still get an error
Unable to perform assignment because brace indexing is not supported for variables of this type.
Error in loop_code (line 9)
d{k}= uint8(b) .* uint8(b>180);

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeScope Variables and Generate Names についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by