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

조회 수: 1 (최근 30일)
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
Adam
Adam 2014년 9월 12일
Please format your code by selecting it all and clicking the '{} Code' button
Sagi
Sagi 2014년 9월 12일
% --- 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

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2014년 9월 12일
pathname is in ImSelect_Callback() but not in ImPr_Callback().
Either put
global pathname;
  댓글 수: 2
Adam
Adam 2014년 9월 12일
Be very careful using global variables though. They are very easy to misuse and should generally be considered a last resort as in any programming language.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam
Adam 2014년 9월 12일
편집: Adam 2014년 9월 12일
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
Adam
Adam 2014년 9월 12일
편집: Adam 2014년 9월 12일
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.
Sagi
Sagi 2014년 9월 12일
thanks alot Adam for the explanation,I understand it much better now!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by