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:
how can you get data from GUI to workspace?

Subject: how can you get data from GUI to workspace?

From: Adam Chapman

Date: 20 Jun, 2010 13:20:37

Message: 1 of 14

Im trying to make a simple GUI just as a learning exercise, I've never
made one before.

I have made my simple GUI which loads some data and plots it.

Now I want to have that data available i the matlab workspace.

Is there a function to do that or is it more complex?

Subject: how can you get data from GUI to workspace?

From: us

Date: 20 Jun, 2010 13:30:23

Message: 2 of 14

Adam Chapman <adamchapman1985@hotmail.co.uk> wrote in message <a162f378-0ac2-41c1-97a7-989a942f6656@s9g2000yqd.googlegroups.com>...
> Im trying to make a simple GUI just as a learning exercise, I've never
> made one before.
>
> I have made my simple GUI which loads some data and plots it.
>
> Now I want to have that data available i the matlab workspace.
>
> Is there a function to do that or is it more complex?
>

a hint:
- use one of the many data containers, eg,

     set(0,'userdata',...);
     set(gcf,'userdata',...);
     help setappdata; % <- and siblings...

us

Subject: how can you get data from GUI to workspace?

From: Adam Chapman

Date: 20 Jun, 2010 14:12:36

Message: 3 of 14

On Jun 20, 2:30 pm, "us " <u...@neurol.unizh.ch> wrote:
> Adam Chapman <adamchapman1...@hotmail.co.uk> wrote in message <a162f378-0ac2-41c1-97a7-989a942f6...@s9g2000yqd.googlegroups.com>...
> > Im trying to make a simple GUI just as a learning exercise, I've never
> > made one before.
>
> > I have made my simple GUI which loads some data and plots it.
>
> > Now I want to have that data available i the matlab workspace.
>
> > Is there a function to do that or is it more complex?
>
> a hint:
> - use one of the many data containers, eg,
>
>      set(0,'userdata',...);
>      set(gcf,'userdata',...);
>      help setappdata;     % <- and siblings...
>
> us

I appreciate the response but didn't have much luck with that.

Perhaps I should explain the data is not in graphical form. Basically
all the button on my GUI does is load some data from a text file.

It looks as though your suggestion needs the data to be shown in a
figure before copying it to the workspace (correct me if im wrong).

Subject: how can you get data from GUI to workspace?

From: us

Date: 20 Jun, 2010 14:19:04

Message: 4 of 14

Adam Chapman <adamchapman1985@hotmail.co.uk> wrote in message <e1dc2db0-1c21-4c1d-a869-33fa537c1762@k39g2000yqd.googlegroups.com>...
> On Jun 20, 2:30 pm, "us " <u...@neurol.unizh.ch> wrote:
> > Adam Chapman <adamchapman1...@hotmail.co.uk> wrote in message <a162f378-0ac2-41c1-97a7-989a942f6...@s9g2000yqd.googlegroups.com>...
> > > Im trying to make a simple GUI just as a learning exercise, I've never
> > > made one before.
> >
> > > I have made my simple GUI which loads some data and plots it.
> >
> > > Now I want to have that data available i the matlab workspace.
> >
> > > Is there a function to do that or is it more complex?
> >
> > a hint:
> > - use one of the many data containers, eg,
> >
> >      set(0,'userdata',...);
> >      set(gcf,'userdata',...);
> >      help setappdata;     % <- and siblings...
> >
> > us
>
> I appreciate the response but didn't have much luck with that.
>
> Perhaps I should explain the data is not in graphical form. Basically
> all the button on my GUI does is load some data from a text file.
>
> It looks as though your suggestion needs the data to be shown in a
> figure before copying it to the workspace (correct me if im wrong).

NO, not at all...
better have a second look, eg,

     set(0,'userdata',...); % <- the root is NOT a fig...
% did you look at this at all(?)...
     help setappdata; % <- and siblings...

us

Subject: how can you get data from GUI to workspace?

From: Adam Chapman

Date: 20 Jun, 2010 14:32:36

Message: 5 of 14

On Jun 20, 3:19 pm, "us " <u...@neurol.unizh.ch> wrote:
> Adam Chapman <adamchapman1...@hotmail.co.uk> wrote in message <e1dc2db0-1c21-4c1d-a869-33fa537c1...@k39g2000yqd.googlegroups.com>...
> > On Jun 20, 2:30 pm, "us " <u...@neurol.unizh.ch> wrote:
> > > Adam Chapman <adamchapman1...@hotmail.co.uk> wrote in message <a162f378-0ac2-41c1-97a7-989a942f6...@s9g2000yqd.googlegroups.com>...
> > > > Im trying to make a simple GUI just as a learning exercise, I've never
> > > > made one before.
>
> > > > I have made my simple GUI which loads some data and plots it.
>
> > > > Now I want to have that data available i the matlab workspace.
>
> > > > Is there a function to do that or is it more complex?
>
> > > a hint:
> > > - use one of the many data containers, eg,
>
> > >      set(0,'userdata',...);
> > >      set(gcf,'userdata',...);
> > >      help setappdata;     % <- and siblings...
>
> > > us
>
> > I appreciate the response but didn't have much luck with that.
>
> > Perhaps I should explain the data is not in graphical form. Basically
> > all the button on my GUI does is load some data from a text file.
>
> > It looks as though your suggestion needs the data to be shown in a
> > figure before copying it to the workspace (correct me if im wrong).
>
> NO, not at all...
> better have a second look, eg,
>
>      set(0,'userdata',...);     % <- the root is NOT a fig...
> % did you look at this at all(?)...
>      help setappdata;     % <- and siblings...
>
> us- Hide quoted text -
>
> - Show quoted text -

I have a variable called 'V' that is loaded from the text file with
one of my button callback functions.

Ive tried:
h=setappdata(0,'V',V) and h=set(0,'V',V)

but to no avail.

The first line of my GUI script is:
function [t V F]=myGUI

do i need to scrap the output format and use:

function h=myGUI

?

Subject: how can you get data from GUI to workspace?

From: us

Date: 20 Jun, 2010 14:43:04

Message: 6 of 14

Adam Chapman
> Ive tried:
> h=setappdata(0,'V',V) and h=set(0,'V',V)
>
> but to no avail.

well... of course, this does CANNOT work - and the syntax is wrong, to boot...

did you carefully look at what i've shown to you(?)...
did you, by any chance, see that i used something like ...'userdata'... in the SET
     example
did you peruse the HELP/DOC(?)...
did you look at the ...siblings... of SETAPPDATA, in particular: GETAPPDATA...

did you do anything like that(?)...

us

Subject: how can you get data from GUI to workspace?

From: Adam Chapman

Date: 20 Jun, 2010 15:11:18

Message: 7 of 14

Im sorry, Im not trying to annoy you but I actually dont understand
this.

I have looked at the help and understand that 'userdata' can be any
name you want to call your variable that you want to transfer.

Now in the help the format is specified:
setappdata(h,'name',value)

Where h is a handle to an object. I guess you would make a handle to
an object called 'userdata'.

so now my code snippet in the GUI looks like this:

h=set(0,'data');
setappdata(h,'data',V);

now I supose the function getappdata is called from the command
window? However the handle h isn't accessible in the command window.
Now my brain hurts!

Subject: how can you get data from GUI to workspace?

From: us

Date: 20 Jun, 2010 15:29:04

Message: 8 of 14

Adam Chapman <adamchapman1985@hotmail.co.uk> wrote in message <a6463f72-e9ac-4660-8085-939c9512d342@k39g2000yqd.googlegroups.com>...
> Im sorry, Im not trying to annoy you but I actually dont understand
> this.
>
> I have looked at the help and understand that 'userdata' can be any
> name you want to call your variable that you want to transfer.
>
> Now in the help the format is specified:
> setappdata(h,'name',value)
>
> Where h is a handle to an object. I guess you would make a handle to
> an object called 'userdata'.
>
> so now my code snippet in the GUI looks like this:
>
> h=set(0,'data');
> setappdata(h,'data',V);
>
> now I supose the function getappdata is called from the command
> window? However the handle h isn't accessible in the command window.
> Now my brain hurts!

oh well...
NO...
you REALLY should read the get-started docs, which are very well written...

1) 'userdata' is the common(!) property name for a graphics obj, eg, a figure, axis, line, etc...
thus, the syntax ALWAYS follows this - exemplary - logic

     oh=line; % <- or any other object handle...
     set(oh,'userdata',{magic(3),'foo'});
% then, later on (as long as you have access to OH[!])...
     ud=get(oh,'userdata');

2) SET-/GET- APPDATA typically are used like this

% in your function, eg, a callback...
     setappdata(0,'thisdata',{magic(3),'foo'}); % <- we use the root's container!
% then, later on in anoter function or the command window...
     ud=getappdata(0,'thisdata');
% note: the root's handle (0) can be replaced by any other OH - as shown above...

us

Subject: how can you get data from GUI to workspace?

From: Adam Chapman

Date: 20 Jun, 2010 16:10:23

Message: 9 of 14

On Jun 20, 4:29 pm, "us " <u...@neurol.unizh.ch> wrote:
> Adam Chapman <adamchapman1...@hotmail.co.uk> wrote in message <a6463f72-e9ac-4660-8085-939c9512d...@k39g2000yqd.googlegroups.com>...
> > Im sorry, Im not trying to annoy you but I actually dont understand
> > this.
>
> > I have looked at the help and understand that 'userdata' can be any
> > name you want to call your variable that you want to transfer.
>
> > Now in the help the format is specified:
> > setappdata(h,'name',value)
>
> > Where h is a handle to an object. I guess you would make a handle to
> > an object called 'userdata'.
>
> > so now my code snippet in the GUI looks like this:
>
> > h=set(0,'data');
> > setappdata(h,'data',V);
>
> > now I supose the function getappdata is called from the command
> > window? However the handle h isn't accessible in the command window.
> > Now my brain hurts!
>
> oh well...
> NO...
> you REALLY should read the get-started docs, which are very well written...
>
> 1) 'userdata' is the common(!) property name for a graphics obj, eg, a figure, axis, line, etc...
> thus, the syntax ALWAYS follows this - exemplary - logic
>
>      oh=line;     % <- or any other object handle...
>      set(oh,'userdata',{magic(3),'foo'});
> % then, later on (as long as you have access to OH[!])...
>      ud=get(oh,'userdata');
>
> 2) SET-/GET- APPDATA typically are used like this
>
> % in your function, eg, a callback...
>      setappdata(0,'thisdata',{magic(3),'foo'});     % <- we use the root's container!
> % then, later on in anoter function or the command window...
>      ud=getappdata(0,'thisdata');
> % note: the root's handle (0) can be replaced by any other OH - as shown above...
>
> us- Hide quoted text -
>
> - Show quoted text -

Thankyou, that was a big help.

Is there any way to actually force the data into the normal workspace
though without having to call it in?

Subject: how can you get data from GUI to workspace?

From: Adam Chapman

Date: 20 Jun, 2010 16:13:02

Message: 10 of 14

On Jun 20, 5:10 pm, Adam Chapman <adamchapman1...@hotmail.co.uk>
wrote:
> On Jun 20, 4:29 pm, "us " <u...@neurol.unizh.ch> wrote:
>
>
>
>
>
> > Adam Chapman <adamchapman1...@hotmail.co.uk> wrote in message <a6463f72-e9ac-4660-8085-939c9512d...@k39g2000yqd.googlegroups.com>...
> > > Im sorry, Im not trying to annoy you but I actually dont understand
> > > this.
>
> > > I have looked at the help and understand that 'userdata' can be any
> > > name you want to call your variable that you want to transfer.
>
> > > Now in the help the format is specified:
> > > setappdata(h,'name',value)
>
> > > Where h is a handle to an object. I guess you would make a handle to
> > > an object called 'userdata'.
>
> > > so now my code snippet in the GUI looks like this:
>
> > > h=set(0,'data');
> > > setappdata(h,'data',V);
>
> > > now I supose the function getappdata is called from the command
> > > window? However the handle h isn't accessible in the command window.
> > > Now my brain hurts!
>
> > oh well...
> > NO...
> > you REALLY should read the get-started docs, which are very well written...
>
> > 1) 'userdata' is the common(!) property name for a graphics obj, eg, a figure, axis, line, etc...
> > thus, the syntax ALWAYS follows this - exemplary - logic
>
> >      oh=line;     % <- or any other object handle...
> >      set(oh,'userdata',{magic(3),'foo'});
> > % then, later on (as long as you have access to OH[!])...
> >      ud=get(oh,'userdata');
>
> > 2) SET-/GET- APPDATA typically are used like this
>
> > % in your function, eg, a callback...
> >      setappdata(0,'thisdata',{magic(3),'foo'});     % <- we use the root's container!
> > % then, later on in anoter function or the command window...
> >      ud=getappdata(0,'thisdata');
> > % note: the root's handle (0) can be replaced by any other OH - as shown above...
>
> > us- Hide quoted text -
>
> > - Show quoted text -
>
> Thankyou, that was a big help.
>
> Is there any way to actually force the data into the normal workspace
> though without having to call it in?- Hide quoted text -
>
> - Show quoted text -

Neever mind, found it: assignin does the trick

Thanks again for your help us, I appreciate your time

Subject: how can you get data from GUI to workspace?

From: us

Date: 20 Jun, 2010 17:02:05

Message: 11 of 14

Adam Chapman
> Neever mind, found it: assignin does the trick
>
> Thanks again for your help us, I appreciate your time

ok, then: let me say this very bluntly:
this is the most unintelligent(!) solution to your well-known problem and most CSSMers will not understand why you did not take the time to learn from what was told and taught you today...
this is not about being ...trick(y)... it's about mastering ML...

us

Subject: how can you get data from GUI to workspace?

From: Rodney Thomson

Date: 21 Jun, 2010 02:08:06

Message: 12 of 14

"us " <us@neurol.unizh.ch> wrote in message <hvlhid$4en$1@fred.mathworks.com>...
> Adam Chapman
> > Neever mind, found it: assignin does the trick
> >
> > Thanks again for your help us, I appreciate your time
>
> ok, then: let me say this very bluntly:
> this is the most unintelligent(!) solution to your well-known problem and most CSSMers will not understand why you did not take the time to learn from what was told and taught you today...
> this is not about being ...trick(y)... it's about mastering ML...
>
> us

**Disclaimer - I've not written UIs since R14 so there may be even better ways of doing this these days

Looking at this thread there seem to be a lot of complicated methods suggested to get the GUI to return some data to the user workspace.

Hasn't anyone simply thought to use the normal function return syntax? Same as you would any other function? ie:

data = loadDataGUI();

The mechanism to perform this in the GUI.m code is rather simple.

1) When loading your data (ie clicking on a button, selecting a file, parsing data etc), store this within the handles structure:
function load_pushButton_Callback(hObject, eventdata, handles)
  data = dlmread('data.txt'); % or wherever your data comes from
  handles.data = data;
  guidata(hObject, data);
end

2) in your OutputFunction assign the data variable to an output argument (the default blank UI in GUIDE even has this as an example with 'handles.output'!)"

varargout{1} = handles.data;

3) Profit

Cheers

Rod

--
http://iheartmatlab.blogspot.com

Subject: how can you get data from GUI to workspace?

From: us

Date: 21 Jun, 2010 02:22:05

Message: 13 of 14

"Rodney Thomson" <readmore@iheartmatlab.blogspot.com> wrote in message <hvmhi6$620$1@fred.mathworks.com>...
> "us " <us@neurol.unizh.ch> wrote in message <hvlhid$4en$1@fred.mathworks.com>...
> > Adam Chapman
> > > Neever mind, found it: assignin does the trick
> > >
> > > Thanks again for your help us, I appreciate your time
> >
> > ok, then: let me say this very bluntly:
> > this is the most unintelligent(!) solution to your well-known problem and most CSSMers will not understand why you did not take the time to learn from what was told and taught you today...
> > this is not about being ...trick(y)... it's about mastering ML...
> >
> > us
>
> **Disclaimer - I've not written UIs since R14 so there may be even better ways of doing this these days
>
> Looking at this thread there seem to be a lot of complicated methods suggested to get the GUI to return some data to the user workspace.
>
> Hasn't anyone simply thought to use the normal function return syntax? Same as you would any other function? ie:
>
> data = loadDataGUI();
>
> The mechanism to perform this in the GUI.m code is rather simple.
>
> 1) When loading your data (ie clicking on a button, selecting a file, parsing data etc), store this within the handles structure:
> function load_pushButton_Callback(hObject, eventdata, handles)
> data = dlmread('data.txt'); % or wherever your data comes from
> handles.data = data;
> guidata(hObject, data);
> end
>
> 2) in your OutputFunction assign the data variable to an output argument (the default blank UI in GUIDE even has this as an example with 'handles.output'!)"
>
> varargout{1} = handles.data;
>
> 3) Profit
>
> Cheers
>
> Rod
>
> --
> http://iheartmatlab.blogspot.com

well... yes... GUIDATA is just another one of these containers (that the OP apparently cannot 'handle')...
the downside: it's much more limited...
so: use with caution...

us

Subject: how can you get data from GUI to workspace?

From: Doug Weathers

Date: 22 Jul, 2010 20:23:03

Message: 14 of 14

Hey, thanks! That's just what I was looking for.

- Doug


"Rodney Thomson" <readmore@iheartmatlab.blogspot.com> wrote in message <hvmhi6$620$1@fred.mathworks.com>...

>
> **Disclaimer - I've not written UIs since R14 so there may be even better ways of doing this these days
>
> Looking at this thread there seem to be a lot of complicated methods suggested to get the GUI to return some data to the user workspace.
>
> Hasn't anyone simply thought to use the normal function return syntax? Same as you would any other function? ie:
>
> data = loadDataGUI();
>
> The mechanism to perform this in the GUI.m code is rather simple.
>
> 1) When loading your data (ie clicking on a button, selecting a file, parsing data etc), store this within the handles structure:
> function load_pushButton_Callback(hObject, eventdata, handles)
> data = dlmread('data.txt'); % or wherever your data comes from
> handles.data = data;
> guidata(hObject, data);
> end
>
> 2) in your OutputFunction assign the data variable to an output argument (the default blank UI in GUIDE even has this as an example with 'handles.output'!)"
>
> varargout{1} = handles.data;
>
> 3) Profit
>
> Cheers
>
> Rod
>
> --
> http://iheartmatlab.blogspot.com

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