From: Ish Khan on
Hi, I am trying to create a GUI which will do the following:
On clincking the 1st push button, it will enable the user the upload a image and display it onto the axes.
on clicking the 2nd push button, it will calculate some values (like area) of the image the user uploaded, and than able to display the results,
This is my code for push button 1:
[FileName, folder] = uigetfile({'*.*'}, 'Select file');
if FileName ~= 0
fullName = fullfile(folder,FileName);
end
J = imread(fullName);
imshow(J, 'Parent', handles.axes1); [Thanks to Walter and ImageAnalyst for help with the code.]

on pushing the 2nd push button, it should take the image, fullName, and calculate the values, but when i try to execute the 2nd push button, the error comes that 'Undefined function or variable 'fullName'.' but i have already defined it in push button 1 isn't it? what am I doing wrong? any kind advice will be very much appreciated:)
From: Matt Fig on
Unless you are using nested functions, the variables defined in one callback do not exist in another. Thus you will need to store them somewhere. You might try GUIDATA.
From: Iman on
"Ish Khan" <smitten790(a)yahoo.com> wrote in message <hu4ec9$t55$1(a)fred.mathworks.com>...
> Hi, I am trying to create a GUI which will do the following:
> On clincking the 1st push button, it will enable the user the upload a image and display it onto the axes.
> on clicking the 2nd push button, it will calculate some values (like area) of the image the user uploaded, and than able to display the results,
> This is my code for push button 1:
> [FileName, folder] = uigetfile({'*.*'}, 'Select file');
> if FileName ~= 0
> fullName = fullfile(folder,FileName);
> end
> J = imread(fullName);
> imshow(J, 'Parent', handles.axes1); [Thanks to Walter and ImageAnalyst for help with the code.]
>
> on pushing the 2nd push button, it should take the image, fullName, and calculate the values, but when i try to execute the 2nd push button, the error comes that 'Undefined function or variable 'fullName'.' but i have already defined it in push button 1 isn't it? what am I doing wrong? any kind advice will be very much appreciated:)

If you have used guide to create the gui, the calback functions of pushbutton 1 and push button2 cannot see the variables of each other, so have to transfere variables between them, by saving someware and then call back in the calback of bushbutton, so: try the following

IF the tags of pushbuttons are pb1 andd pb2 and the handles of them are respectively:
handles.pb1, handles.pb2
you can try the following
at the end of the the calback function of pb1

function pb1_callback(hobject,handles,eventdata)
[FileName, folder] = uigetfile({'*.*'}, 'Select file');
if FileName ~= 0
fullName = fullfile(folder,FileName);
end
J = imread(fullName);
mshow(J, 'Parent', handles.axes1);
% save fullName in the userdata of pb1
set(handles.pb1,'userdata','fullName')
% Update handles structure
guidata(hObject, handles);
and in the start of the callback of the second push button add
fullName = get(handles.pb1,'userdata');

and continue

I hope this helps
From: Ish Khan on
"Iman " <imanabdelhamid(a)yahho.com> wrote in message <hu8oeb$5oc$1(a)fred.mathworks.com>...
> "Ish Khan" <smitten790(a)yahoo.com> wrote in message <hu4ec9$t55$1(a)fred.mathworks.com>...
> > Hi, I am trying to create a GUI which will do the following:
> > On clincking the 1st push button, it will enable the user the upload a image and display it onto the axes.
> > on clicking the 2nd push button, it will calculate some values (like area) of the image the user uploaded, and than able to display the results,
> > This is my code for push button 1:
> > [FileName, folder] = uigetfile({'*.*'}, 'Select file');
> > if FileName ~= 0
> > fullName = fullfile(folder,FileName);
> > end
> > J = imread(fullName);
> > imshow(J, 'Parent', handles.axes1); [Thanks to Walter and ImageAnalyst for help with the code.]
> >
> > on pushing the 2nd push button, it should take the image, fullName, and calculate the values, but when i try to execute the 2nd push button, the error comes that 'Undefined function or variable 'fullName'.' but i have already defined it in push button 1 isn't it? what am I doing wrong? any kind advice will be very much appreciated:)
>
> If you have used guide to create the gui, the calback functions of pushbutton 1 and push button2 cannot see the variables of each other, so have to transfere variables between them, by saving someware and then call back in the calback of bushbutton, so: try the following
>
> IF the tags of pushbuttons are pb1 andd pb2 and the handles of them are respectively:
> handles.pb1, handles.pb2
> you can try the following
> at the end of the the calback function of pb1
>
> function pb1_callback(hobject,handles,eventdata)
> [FileName, folder] = uigetfile({'*.*'}, 'Select file');
> if FileName ~= 0
> fullName = fullfile(folder,FileName);
> end
> J = imread(fullName);
> mshow(J, 'Parent', handles.axes1);
> % save fullName in the userdata of pb1
> set(handles.pb1,'userdata','fullName')
> % Update handles structure
> guidata(hObject, handles);
> and in the start of the callback of the second push button add
> fullName = get(handles.pb1,'userdata');
>
> and continue
>
> I hope this helps

Hi Iman, Thanks a lot for the code, this is how i code it this time:
For push button 1 :
[baseFileName, folder] = uigetfile({'*.*'}, 'Select file');
if baseFileName ~= 0
fullName = fullfile(folder, baseFileName);
end
imshow(fullName, 'Parent', handles.axes1);
set(handles.pushbutton1,'userdata','fullName')
guidata(hObject, handles);

For push button 2 :
fullName = get(handles.pushbutton1,'userdata');

I = imread (fullName);

The error message says: Error using ==> imread at 361
File "fullName" does not exist.

How can it not exist if it was just uploaded? Thank you very much in advance for any helpful advice:)
From: Christopher on
_____________________________________________________________________
For push button 1 :
[baseFileName, folder] = uigetfile({'*.*'}, 'Select file');
if baseFileName ~= 0
fullName = fullfile(folder, baseFileName);
end
imshow(fullName, 'Parent', handles.axes1);
set(handles.pushbutton1,'userdata','fullName')
guidata(hObject, handles);

For push button 2 :
fullName = get(handles.pushbutton1,'userdata');

I = imread (fullName);

The error message says: Error using ==> imread at 361
File "fullName" does not exist.
_____________________________________________________________________

The problem is in the line:

set(handles.pushbutton1,'userdata','fullName');

Because you've encased the variable fullName in a set of quotations (' ') it is being recognised as a String. If you debugged your code, in the push button 2 callback you will see that:

fullName = get(handles.pushbutton1,'userdata');

will return:

fullName = 'fullName';

So now you're local variable fullName has the String value of 'fullName' and when you attempt to read the 'image' instead of:

imread('C:\Documents and Settings\...');

you will have:

imread('fullName');

which of course does not exist. So then, all you need to do is remove the quotation marks of your original userdata definition, i.e:

set(handles.pushbutton1,'userdata',fullName);

Chris