简体   繁体   中英

How to get the variable value from a function in a same m file in matlab?

I want to make a GUI in matlab. In the (*.fig) file, There will be 2 push buttons. the first is to read the image from the directory, save it into a variable and show it in the matlab GUI.

so in the main file (*.m) i will have 2 functions that represent the 2 buttons.

for example in the main file (*.m), the first function : x=imread('image.bmp');

And the second button (the second function, in the same main file as a first function) is to make a process with a image that have read by the first button. so, i have to get the variable x from the first button (function) to do the process. If i have get the x variable, i can send it to another *.M files.

How to get the x variable from the first function.

Here is my code..

THE MAIN FILE

%First button (Function)
function pushbutton1_Callback(hObject, eventdata, handles)  
% hObject    handle to pushbutton1 (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({'*.bmp';'*.jpg';'*.gif';'*.*'},        
'Pick an Image File');
citra1 = imread([pathname,filename]);
axes(handles.axes1);
imshow(citra1);
handles.citra1 = citra1;
guidata(hObject, handles);
set(handles.text1,'String',filename);

% and here is my second button (function)

function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[gambr1, psan1, jmlobjk1, brs1, klm1]=BacaCitra1(citra1);
 axes(handles.axes3);
 imshow(gambr1);
 handles.gambr1 = gambr1;
 guidata(hObject, handles);

i wanna send the "citra1" as a image variable from the first function to be read in the second function, so i can do

 [gambr1, psan1, jmlobjk1, brs1, klm1]=BacaCitra1(citra1);

but there are error messeges like : Error while evaluating uicontrol Callback

??? Undefined function or variable 'citra1'.
Error in ==> deteksi2citra>pushbutton1_Callback at 117
[gambr1, psan1, jmlobjk1, brs1, klm1]=BacaCitra1(citra1);

 Error in ==> gui_mainfcn at 96
    feval(varargin{:});

  Error in ==> deteksi2citra at 42
gui_mainfcn(gui_State, varargin{:});

 Error in ==>        @(hObject,eventdata)deteksi2citra('pushbutton1_Callback',hObject,even data,guidata(hObject))
 ??? Error while evaluating uicontrol Callback

Thank you for the help :D

It looks like you are already doing what you need to do, in fact, it looks like you are using two methods in parallel. You save the variable citra1 to the handles structure in pushbutton1_Callback and you are accepting that structure as a user variable in both callbacks. You are also saving handles with guidata .

See Sharing Data Among a GUI's Callbacks and guidata to see how you to extract citra1 from handles in either case.

If you are setting the callbacks correctly, including the handles structure, then the short answer is to use handles.citra1 instead of just citra1 in your second callback. I highly recommend reading the callback documentation to understand what is happening, however.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM