MATLAB Answers

0

How can I use local variable from GUI

Asked by Nopparat on 10 Jan 2013
I created subroutine for my code named ' checkbox3' including 3 checkboxes. If I check a checkbox, it will return 1 and if not, it will return 0. For example, If I check the first and third box, I want it to return chk1=1;chk2=0;chk3=1. I really want these variables for my next step. I tried to use assignin function but it went to base workspace instead of my working workspace and I can't use these variable for my next step. So please give me some advice for this problem. Thank you very much! This is my code.
chk1=0;chk2=0;chk3=0;
assignin('base','chk1',chk1);assignin('base','chk2',chk2);... assignin('base','chk3',chk3);
% --- Executes on button press in checkbox1.
function chk1=checkbox1_Callback(hObject, eventdata, handles)
chk1=get(hObject,'Value');
assignin('base','chk1',chk1);
% --- Executes on button press in checkbox2.
function chk2=checkbox2_Callback(hObject, eventdata, handles)
chk2=get(hObject,'Value');
assignin('base','chk2',chk2);
% --- Executes on button press in checkbox3.
function chk3=checkbox3_Callback(hObject, eventdata, handles)
chk3=get(hObject,'Value');
assignin('base','chk3',chk3);
function pushbutton1_Callback(hObject, eventdata, handles)
close all

  0 Comments

Sign in to comment.

2 Answers

Answer by Image Analyst
on 10 Jan 2013
 Accepted Answer

All of that is unnecessary. Just check the values when you need them, or worst case, have a function to retrieve all of their values, like this:
function checkboxValues = GetCheckBoxValues(handles)
checkboxValues (1) = get(handles.checkbox1, 'value');
checkboxValues (2) = get(handles.checkbox2, 'value');
checkboxValues (3) = get(handles.checkbox3, 'value');
Then just call that function whenever you need to get, use, or check the values:
checkboxValues = GetCheckBoxValues(handles);
No need for all of that complicated stuff you were doing in each individual callback.

  1 Comment

Thanks a lot. That's very easy! However, usually for subroutine I can call local variables for using in another subroutine. For example,
function bio=bio_analysis(data1,data2) % main function
......
....
[option1 option2]= checkbox % 1st subroutine ( I failed this step. I couldn't get option1 and option2 from this subroutine to use for another subroutine) this checkbox subroutine is this same one as shown above
[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

Sign in to comment.


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

In opening function
chk123=[0 0 0];
set(handles.checkbox1,'userdata',chk123)
%In checkbox1 function
function chk1=checkbox1_Callback(hObject, eventdata, handles)
chk1=get(hObject,'Value');
chk123=get(handles.checkbox1,'userdata');
chk123(1)=chk1; % for chk2 it will be chk123(2)=chk2
set(handles.checkbox1,'userdata',chk123)
%or use guidata
%In opening function,
chk=[0 0 0];
handles.chk=chk;
guidata(hObject, handles);
%In checkbox1 function
chk1=get(hObject,'Value');
chk(1)=chk1;
handles.chk=chk
guidata(hObject, handles);

  3 Comments

Thank you very much for your suggestion. I tried both of your solutions but I still couldn't use variables for next step. I must do something wrong. Please take a look what I did
% --- Executes just before checkbox3 is made visible.
function checkbox3_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
chk123=[0 0 0];
set(handles.checkbox1,'userdata',chk123)
% --- 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 chk1=checkbox1_Callback(hObject, eventdata, handles)
chk1=get(hObject,'Value');
chk123=get(handles.checkbox1,'userdata');
chk123(1)=chk1;
set(handles.checkbox1,'userdata',chk123)
% --- Executes on button press in checkbox2.
function chk2=checkbox2_Callback(hObject, eventdata, handles)
chk2=get(hObject,'Value');
chk123=get(handles.checkbox1,'userdata');
chk123(2)=chk2;
set(handles.checkbox1,'userdata',chk123)
% --- Executes on button press in checkbox3.
function chk3=checkbox3_Callback(hObject, eventdata, handles)
chk3=get(hObject,'Value');
% --- Executes on button press in pushbutton1.
chk123=get(handles.checkbox1,'userdata');
chk123(2)=chk2;
set(handles.checkbox1,'userdata',chk123)
function pushbutton1_Callback(hObject, eventdata, handles)
a= chk(1); % error !!!
close all
I still can't use variables for my next step. I think I must do something wrong! Thank you in advance
In the last function
function pushbutton1_Callback(hObject, eventdata, handles)
chk=get(handles.checkbox1,'userdata');
a= chk(1); % error !!!
close all
% you also did an error in checkbox3_Callback, it's
chk123(3)=chk3 % instead of chk123(2)=chk2
Thanks a lot! it works

Sign in to comment.