Image Processing Downloading Images for GUI

I'm learning to create GUI's for image processing. I found a youtube video that demonstrates what I need to know, but I'm having an issue. I created a push button ( that I've titled import image) and upon clicking the image a window pops up for me to choose the file (image) that I would like to show up on the GUI plot. When I try choosing a file (image) an error message pops up. The error message states: Reference to non-existent field 'image_ax'. How do I know what to use instead of 'image_ax'? I simply copied that part from the video. I pasted my code below
a = uigetfile() filename = a; setappdata(0,'filename',filename); a = imread(a); axes(handles.image_ax); imshow(a); setappdata(0,'a',a) setappdata(0,'filename',a); plot(handles.image_ax,'a');

3 Comments

Adam
Adam on 14 Aug 2018
Edited: Adam on 14 Aug 2018
If you didn't change the default tag then your axes in GUIDE will generally be handles.axes1, handles.axes2, etc. Look at the 'Tag' property of a component to see how to refer to it. For axes you will see this easily in GUIDE itself on top of the axes. For other components look in the properties.
The imshow command doesn't support a target, so the call to axes makes sure that the axes object in your GUI is the current axis. To use this code, you must have already created an axes object and saved the handle to handles.image_ax. You should have a call to guidata somewhere to load the handles struct, or have the handle available using another method (like e.g. with getappdata).
imshow( a, 'parent', handles.image_ax )
is better though too to give an explicit parent.

Sign in to comment.

Answers (1)

The usage of "a" is confusing in your code:
% Your code:
a = uigetfile();
filename = a;
setappdata(0,'filename',filename);
a = imread(a);
axes(handles.image_ax);
imshow(a);
setappdata(0,'a',a)
setappdata(0,'filename',a);
plot(handles.image_ax,'a');
Storing variables in the root's application data is a bad idea at all, because it suffers from the same problems as global variables. Here "a" and "filename" are mixed and store repeatedly. A cleaner version, if there is really a good reason to store the file name globally:
[FileName, FilePath] = uigetfile();
File = fullfile(FilePath, FileName);
setappdata(0, 'File', File);
img = imread(File);
imshow(a, 'Parent', handles.axes1);
This is not useful - why plotting the letter 'a'?
plot(handles.image_ax,'a');
As mentioned by Adam and Rik already, you have to change "image_ax" to the field name you use. We cannot guess, how it is called, but you can use the debugger. Set a breakpoint in this line and check the contents of the handles struct.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Asked:

on 14 Aug 2018

Answered:

Jan
on 14 Aug 2018

Community Treasure Hunt

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

Start Hunting!