MATLAB Answers

0

How can I use local variable from GUI (from subroutine to another subroutine in a main function)

Asked by Nopparat on 11 Jan 2013
How can I use local variable from GUI? I want to use from subroutine to another subroutine in a main function. For example,
function bio=bio_analysis(data1,data2) % my main function
......
....
[option1 option2 option3]= checkbox % my 1st subroutine (This subroutine was created from GUI as shown at the end. I failed this step. I couldn't get option1,option2,and option3 from this subroutine to use for another subroutine)
[info1 info2]= calculation(data1,data2) % 2nd subroutine
....
...
and so on.
end
How could I fix this problem? Thank you.
What's more, I tried to put 2nd subroutine into 1st one(in checkbox) like this (last function in my checkbox subroutine)
function pushbutton1_Callback(hObject, eventdata, handles,data1,data2) % I can't call the data1 and data2 from main function in this subroutine even though I put it in the argument so this function failed!
if checkboxvalue1==1
[data_a]=bottomplane(data1);
else
end
if checkboxvalue2==1
[data_b]=scape_bottom(data2);
else
end
Note: FYI; my checkbox code (GUI)
function varargout = checkbox3(varargin)
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @checkbox3_OpeningFcn, ... 'gui_OutputFcn', @checkbox3_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end
if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT
% --- Executes just before checkbox3 is made visible.
function checkbox3_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for checkbox3
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = checkbox3_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
chk1=get(hObject,'Value');
% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
chk2=get(hObject,'Value');
% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles)
chk3=get(hObject,'Value');
% --- Executes on button press in pushbutton1.
function [O1 O2 O3]= pushbutton1_Callback(hObject, eventdata, handles)
checkboxValues(1) = get(handles.checkbox1, 'value');
checkboxValues(2) = get(handles.checkbox2, 'value');
checkboxValues(3) = get(handles.checkbox3, 'value');
O1=checkboxValues(1); %bottomplan O2=checkboxValues(2); %scape_bottom O3=checkboxValues(3); %despeck2
close all
if O1==1
[data1]=bottomplane(data);
else
end
if O2==1
[data2]=scape_bottom(data);
else
end

1 Answer

Answer by Azzi Abdelmalek
on 11 Jan 2013
Edited by Azzi Abdelmalek
on 11 Jan 2013

Use guidata. for example to communicate y to other functions
y=10
handles.y=y
guidata(hObject, handles);
%to get y
y=handles.y

  4 Comments

Show 1 older comment
The name of your function should be
function checkbox1_Callback(hObject, eventdata, handles)
% handles should be in input argument of your functions
I'm not sure whether I correctly understand or not. In my code, I have only 1 main function and other functions are subroutine that I didn't use function command. For example,
function bio=ana_bio(data1,data2) % this is my main function that use function command
[info1 info2]= calculate1(data1,data2) % my subroutine (for my subroutine I didn't use function command)
[info3 info4]= calculate2(info1,info2)
[o1 o2 o3]= checkbox3 % this subroutine is my problem because I use gui and I can't get o1 o2 and o3 from this subroutine.
So do I need to input like this [o1 o2 o3]= checkbox3(hObeject, eventdat, handles) ? or something else? I'm so confused about gui. This is my first time to use gui. I tried to read information and some examples but I still didn't get it. Thank you very much for your help. I really appreciate it
If you need O1 in your function calculate1, add handles to your input argument
[info1 info2]= calculate1(data1,data2,handles)
o1=handles.O1

Sign in to comment.