MATLAB Answers

Reference to non-existent field 'image'. Error in Untitled2 (line 10) a80 = double(x1_image.image); -----Need help

3 views (last 30 days)
Aris John
Aris John on 13 May 2020
Answered: Aris John on 26 May 2020
A =imread('0001BG.tiff')
save('80s', 'A')
[x_file, x_folder] = uigetfile('*.mat', 'Select 80s-file');
x1_image = load([x_folder,x_file]);
cd(x_folder);
a80 = zeros(1024);
a80 = double(x1_image.image);

  1 Comment

Aris John
Aris John on 13 May 2020
I want to convert a .tiff file to a .mat file 80s, then load it into an empty array a80. The a80 initially created by zeros(1024). While executing the last line it shows an error as follows.
Error in Untitled2 (line 10)
a80 = double(x1_image.image);
I am new to matlab, please help.

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 14 May 2020
Edited: Geoff Hayes on 14 May 2020
Aris - what make you think that image is a valid field for x1_image? In your example, you save the A variable to the 80s.mat file and so when you load this file as
x1_image = load([x_folder,x_file]);
then the field for x1_image will be A. Your code would then be
A =imread('0001BG.tiff')
save('80s', 'A')
[x_file, x_folder] = uigetfile('*.mat', 'Select 80s-file');
x1_image = load([x_folder,x_file]);
cd(x_folder);
a80 = zeros(1024);
a80 = double(x1_image.A);
The above code is only valid if A is the variable that is always saved to the 80s.mat files. You could use fieldnames to get the list of fields of x1_image and your code might become
A =imread('0001BG.tiff')
save('80s', 'A')
[x_file, x_folder] = uigetfile('*.mat', 'Select 80s-file');
x1_image = load([x_folder,x_file]);
structFieldnames = fieldnames(x1_image);
if ~isempty(structFieldnames)
cd(x_folder);
a80 = zeros(1024);
a80 = double(getfield(x1_image, structFieldnames{1}));
end
Here I'm assuming that the first fieldname corresponds to the image of interest...

  1 Comment

Walter Roberson
Walter Roberson on 14 May 2020
[x_file, x_folder] = uigetfile('*.mat', 'Select 80s-file');
if ~ischar(x_file); return; end %user cancel
x1_image = load( fullfile(x_folder, x_file) );
uigetfile does not promise that the returned folder will end in a path separator character, and it does not promise to be consistent about that either. A couple of weeks ago, someone on a Windows system was having trouble and it turned out that uigetfile was not putting in the path seperator. Using fullfile() makes it unecessary to think about whether the separator is there are not.

Sign in to comment.


Translated by