MATLAB: Save image using Guide GUI does not work

gui image saveImage Processing Toolbox

When trying to save an image from my GUI I get the error:
—————————————————————————–
Error using imwrite (line 442)
Expected DATA to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64, logical
Instead its type was matlab.graphics.axis.Axes.
——————————————————————————————-
I want to save an image that I modified, where I added squares and/or other images (see image). This is the code I am using:
first_c= 'C:\Users\';
user_name= getenv('username');
second_c='\Desktop';
com_path= fullfile(first_c,user_name,second_c);
a= handles.axes1;
defaultFileName = fullfile(com_path, '*.png*');
[baseFileName, folder] = uiputfile(defaultFileName, 'Specify a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName);
imwrite(a, fullFileName);
————————————————————————————–
Can someone please help me? It is for an important project.

Best Answer

You put some variable into the axes. So you don't need to get that variable back out of that axes -- you can jsut use the same variable you loaded into it. If you did want to get the image out, you'd use getimage()
displayedImage = getimage(handles.axes1);
% Then create fullFileName. Then save.
imwrite(displayedImage, fullFileName);
But that won't get any graphics you drew over it in the overlay (lines, etc.). To get that you can use exportgraphics().