Attempt to reference field of non-structure array error message

3 views (last 30 days)
I am trying to plot histogram of n image through the below code. Images is 1xn cell
for i = 1:length(Images)
variable=Images{i}.img; %%error :Attempt to reference field of non-structure array
histogram(variable);
end
histogram(Images{2}.img); --this works
I know that the variable should be converted to string here as the file name abc.png (using a dot opertator)will be treated as class, but not sure what has to be done for that .

Answers (2)

Image Analyst
Image Analyst on 20 Oct 2015
How did you stuff data into the Images cell array. I need to see that. Normally you don't add .anything after a cell reference. Extract the structure first, then get the field:
cellContents = Images{i}; % cellContents is a structure, I guess.
myImage = cellContents.img; % Take the "img" field of cellContents structure and make it into its own variable.
But I'm not really sure until I see how you made the Images cell array.

Walter Roberson
Walter Roberson on 20 Oct 2015
Your Images{i} is a struct array. When you access the .img field you are asking for the .img field of all of the array entries; that requires multiple outputs but you only have one output.

Community Treasure Hunt

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

Start Hunting!