Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
GUI : sharing variable between callback

Subject: GUI : sharing variable between callback

From: Bruce Crochat

Date: 8 Apr, 2009 09:46:01

Message: 1 of 7

Hi,

I'm trying to share a variable created in a callback with another callback.

I've tried with the function guidata but I'm not sure I've use it correctly, even if I've looked at the help...

So in my first callback, I'm loading an xls to my gui and store it in a uitable :

function load_xls_Callback(hObject, eventdata, handles)
[file path] = uigetfile('.xls');
data = xlsread([path '/' file]);
[nb_row,nb_col] = size(data)
guidata(hObject, handles);
set(handles.uitable1,'data', data);

Now I want to use with a different callback the variable "data" who contain the content of the xls. So first I have to retrieve it and then I want to be able to use it... But the following code isn't working...

function dcc_Callback(hObject, eventdata, handles)
data_2=guidata(hObject);
returns_data=price2ret(data_2);

Thanks a lot in advance for your precious help

Subject: GUI : sharing variable between callback

From: Omur

Date: 8 Apr, 2009 10:22:00

Message: 2 of 7

"Bruce Crochat" <crochat4@etu.unige.ch> wrote in message <grhrop$42m$1@fred.mathworks.com>...
> Hi,
>
> I'm trying to share a variable created in a callback with another callback.
>
> I've tried with the function guidata but I'm not sure I've use it correctly, even if I've looked at the help...
>
> So in my first callback, I'm loading an xls to my gui and store it in a uitable :
>
> function load_xls_Callback(hObject, eventdata, handles)
> [file path] = uigetfile('.xls');
> data = xlsread([path '/' file]);
> [nb_row,nb_col] = size(data)
> guidata(hObject, handles);
> set(handles.uitable1,'data', data);
>
> Now I want to use with a different callback the variable "data" who contain the content of the xls. So first I have to retrieve it and then I want to be able to use it... But the following code isn't working...
>
> function dcc_Callback(hObject, eventdata, handles)
> data_2=guidata(hObject);
> returns_data=price2ret(data_2);
>
> Thanks a lot in advance for your precious help

To use guidata, make your variable part of the handles structure, that is,
handles.data=data;
guidata(hObject, handles);

then in the other callback
data=handles.data; (no need to call guidata here)

HTH,
Omur
www.promodsoftware.com.tr

Subject: GUI : sharing variable between callback

From: Ahmet Mert

Date: 8 Apr, 2009 10:26:37

Message: 3 of 7

You can use "global" command to define variables.

or you can use handles structure

---------------------------------
For example:

function calc_Callback(hObject, eventdata, handles)

x = 0:100;
y = -100:0;
handles.x =x;
handles.y =y;
guidata(hObject, handles)


function draww_Callback(hObject, eventdata, handles)
plot(handles.x,handles.y)
---------------------------------

Subject: GUI : sharing variable between callback

From: Thomas Clark

Date: 8 Apr, 2009 10:30:18

Message: 4 of 7

Bruce,

You can store your data in the 'handles' structure which is passed around the different callbacks.

To do this, make the call to guidata() after you've loaded your data:

function [] = callback_no_1(hObject, eventdata, handles)

% Load your data
[file path] = uigetfile('.xls');
data = xlsread([path '/' file]);

% Add your data to the handles structure
handles.data = data;

% Your data is now in the handles structure. However, this is currently loaded inot a copy of the handles structure which is local to the current callback. To make this handles structure available to all the other callbacks, you need to update the guidata:
guidata(hObject, handles);

% Your data should be accessible now from a different callback, using:
function [] = callback_no_2(hObject, eventdata, handles)
data = handles.data;

Hope this helps!

Subject: GUI : sharing variable between callback

From: Bruce Crochat

Date: 8 Apr, 2009 11:17:01

Message: 5 of 7

Thanks for all your answers, I now have understand how to use guidata with the handles...

Bruce

Subject: GUI : sharing variable between callback

From: Loren Shure

Date: 8 Apr, 2009 12:34:01

Message: 6 of 7

In article <grhrop$42m$1@fred.mathworks.com>, crochat4@etu.unige.ch
says...
> Hi,
>
> I'm trying to share a variable created in a callback with another callback.
>
> I've tried with the function guidata but I'm not sure I've use it correctly, even if I've looked at the help...
>
> So in my first callback, I'm loading an xls to my gui and store it in a uitable :
>
> function load_xls_Callback(hObject, eventdata, handles)
> [file path] = uigetfile('.xls');
> data = xlsread([path '/' file]);
> [nb_row,nb_col] = size(data)
> guidata(hObject, handles);
> set(handles.uitable1,'data', data);
>
> Now I want to use with a different callback the variable "data" who contain the content of the xls. So first I have to retrieve it and then I want to be able to use it... But the following code isn't working...
>
> function dcc_Callback(hObject, eventdata, handles)
> data_2=guidata(hObject);
> returns_data=price2ret(data_2);
>
> Thanks a lot in advance for your precious help
>

You can use nested functions to share the data. See this:

http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-
tools/

--
Loren
http://blogs.mathworks.com/loren

Subject: GUI : sharing variable between callback

From: Brian

Date: 11 Mar, 2010 16:50:20

Message: 7 of 7

"Thomas Clark" <t.clark@remove.spamcantab.net> wrote in message <grhubq$ph3$1@fred.mathworks.com>...
> Bruce,
>
> You can store your data in the 'handles' structure which is passed around the different callbacks.
>
> To do this, make the call to guidata() after you've loaded your data:
>
> function [] = callback_no_1(hObject, eventdata, handles)
>
> % Load your data
> [file path] = uigetfile('.xls');
> data = xlsread([path '/' file]);
>
> % Add your data to the handles structure
> handles.data = data;
>
> % Your data is now in the handles structure. However, this is currently loaded inot a copy of the handles structure which is local to the current callback. To make this handles structure available to all the other callbacks, you need to update the guidata:
> guidata(hObject, handles);
>
> % Your data should be accessible now from a different callback, using:
> function [] = callback_no_2(hObject, eventdata, handles)
> data = handles.data;
>
> Hope this helps!

_______________________________________________________________
Hello,
This is the exact script I'm running in Matlab. I can't get the variable defined in the save callback to be recognised in the load callback. Any idea what's going wrong with it?

Any clarification would be great.

function init(hObject, eventdata, handles)
handles.fig = figure('units','pixels',...
    'position',[220 150 400 290],...
    'resize','off',...
    'name','Two Body Problem',...
    'numbertitle','off',...
    'color', [0.925,0.914,0.847],...
    'menubar','none');

handles.savetext = uicontrol('style','text',...
    'position',[10 200 110 15],...
    'string','Enter a Number',...
    'visible','on',...
    'HorizontalAlignment','left');

handles.saveedit = uicontrol('style','edit',...
    'position',[90 195 110 30],...
    'backgroundcolor','white',...
    'enable','on',...
    'visible','on');

handles.savepushbutton = uicontrol('style','pushbutton',...
    'position',[205 190 110 40],...
    'string','Click to Save',...
    'visible','on',...
    'HorizontalAlignment','left');

handles.loadpushbutton = uicontrol('style','pushbutton',...
    'position',[10 100 110 40],...
    'string','Click to Load',...
    'visible','on',...
    'HorizontalAlignment','left');

handles.loadtext = uicontrol('style','text',...
    'position',[125 110 110 15],...
    'string','Value Entered',...
    'visible','on',...
    'HorizontalAlignment','left');

set(handles.savepushbutton,'callback',{@save_callback, handles});
set(handles.loadpushbutton, 'callback',{@load_callback, handles});



end

function save_callback(hObject, eventdata, handles)
number=str2num(get(handles.saveedit,'string'));

handles.number=number
guidata(hObject, handles)

msgbox('Variable saved','Saved');

end


function load_callback(hObject, eventdata, handles)
number=handles.number
set(handles.loadtext,'string',num2str(handles.number));

end
                 

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us