Dot indexing error for naming variable

4 visualizzazioni (ultimi 30 giorni)
udi ibgui
udi ibgui il 9 Dic 2019
Commentato: udi ibgui il 10 Dic 2019
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!

Risposte (1)

Walter Roberson
Walter Roberson il 9 Dic 2019
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 Commenti
Stephen23
Stephen23 il 10 Dic 2019
"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 il 10 Dic 2019
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);

Accedi per commentare.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by