in gui,I want to display an image,to use it's path with an other pushbutton and then to display a result in a text edit box

2 views (last 30 days)
that's what i did so far and it doesn't work,i get this error message:
??? Reference to a cleared variable handles.
Error in ==> SimpleGui>ImPr_Callback at 119 set(handles.ImSelect,'String',strcat(pathname,filename));Str=get(handles.ImSelect,'String');
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> SimpleGui at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)SimpleGui('ImPr_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
my code:
% --- Executes on button press in ImSelect. function ImSelect_Callback(hObject, eventdata, handles) % hObject handle to ImSelect (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) [filename,pathname] = uigetfile('*.jpg'); selected=sprintf('%s',filename); i=imread(selected); %reading your .jpg image axis(handles.axes1); %setting the image in axes1 component -look at thag property of axes which is default axes1 imshow(i)
% --- Executes on button press in ImPr. function ImPr_Callback(hObject, eventdata, handles) % hObject handle to ImPr (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) clc,clear,close all %A='C:\Users\Sagipc\Desktop\igal.jpg'; set(handles.ImSelect,'String',strcat(pathname,filename));Str=get(handles.ImSelect,'String'); pic=double(imread(Str,'jpg')); ... ... .. beta=num2str(pd); ... ... set(handles.GetBeta, 'String', beta);
please help.
  2 Comments
Sagi
Sagi on 12 Sep 2014
% --- Executes on button press in ImSelect.
function ImSelect_Callback(hObject, eventdata, handles)
% hObject handle to ImSelect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename,pathname] = uigetfile('*.jpg');
selected=sprintf('%s',filename);
i=imread(selected); %reading your .jpg image
axis(handles.axes1); %setting the image in axes1 component -look at thag property of axes which is default axes1
imshow(i)
% --- Executes on button press in ImPr.
function ImPr_Callback(hObject, eventdata, handles)
% hObject handle to ImPr (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc,clear,close all
%A='C:\Users\Sagipc\Desktop\igal.jpg';
set(handles.ImSelect,'String',strcat(pathname,filename));Str=get(handles.ImSelect,'String');
pic=double(imread(Str,'jpg'));
...
...
...
beta=num2str(pd);
...
...
...
set(handles.GetBeta, 'String', beta);
after doing it , i get this error message:
??? Reference to a cleared variable handles.
Error in ==> SimpleGui>ImPr_Callback at 119 set(handles.ImSelect,'String',strcat(pathname,filename));Str=get(handles.ImSelect,'String');
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> SimpleGui at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)SimpleGui('ImPr_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 12 Sep 2014
pathname is in ImSelect_Callback() but not in ImPr_Callback().
Either put
global pathname;

More Answers (1)

Adam
Adam on 12 Sep 2014
Edited: Adam on 12 Sep 2014
Get rid of
clc,clear,close all
in your ImPr_Callback callback. Or at least just put
close all
if you want to close figures.
A callback has its own workspace independent of the base workspace so clc is un-necessary and clear will remove the callback's input arguments, inclucing 'handles'
  3 Comments
Adam
Adam on 12 Sep 2014
Edited: Adam on 12 Sep 2014
Add:
handles.pathname = pathname;
handles.filename = filename;
guidata( hObject, handles )
to the bottom of your ImSelect_Callback, then pathname and filename will be attached to the GUI's handles data structure (that final line ensures the updated handles structure is stored in the GUI rather than going out of scope when the function ends).
If you prefer you can simply put:
[handles.filename, handles.pathname] = uigetfile('*.jpg');
with
guidata( hObject, handles )
still at the bottom of that callback (basically anywhere after the last assignment to handles, but I like to keep mine as the last instruction of the callback as a rule). With that approach you have to tag 'handles.' to the front of 'filename' wherever it is used in your first callback too though.
Then in your ImPr_Callback you can just use
handles.pathname
instead of just 'pathname'. You can do this for any data you want to share between callbacks.
When you get more used to it you may want to organise things better than just hanging a huge number of variables off the handles structure, but it works and especially when you don't have many variables to share among callbacks.
I would recommend following the link Image Analyst posted above though too. It is always better to understand how things work yourself so that you will know how to apply the solutions in future.

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!