Thread Subject: Function questions

Subject: Function questions

From: Johnathan

Date: 9 May, 2009 07:10:03

Message: 1 of 9

New member, so these questions have probably already been posed and answered.

I have been making a GUI, and I have run into several hangups:

1) Accessing data from other functions:

If i wrote a file like this:

%%%%%%%%%%%%%%%

function function1
f=figure('Name', 'Thing', 'Position', [1,1,500,500])
button1=uicontrol('Style', 'pushbutton', 'Callback',{@function2}, 'Position', [100,100,50,30])

button2=uicontrol('Style', 'pushbutton', 'Position', [200,100,50,30])


function function2(object, eventdata)
get(button1, 'Value')
externalprogram

%%%%%%%%%%%%%%%%

where externalprogram is an m-file like:

%%%%%%%%%%%%%%
x=5
%%%%%%%%%%%%%%

Then I get an error

??? Undefined function or variable 'button1'.

Error in ==> function1>function2 at 9
get(button1, 'Value')
??? Error while evaluating uicontrol Callback

as I should, since the functions have separate workspaces. I know there must be some way to interact with the workspace of function1 from within function2 without running function1 again. When you use GUIDE, there is a nifty get(handles.button1, 'Value') thing that you use, so I guess my question is, how do you construct this handles structure that lets you call on items in different workspaces?


2) If you close the functions as per above with "end"s and nest them, you can say get(button1,'Value'), but when externalprogram tries to run you get a different error:

??? Attempt to add "x" to a static workspace.
 See MATLAB Programming, Restrictions on Assigning to Variables for details.

??? Attempt to add "x" to a static workspace.
 See MATLAB Programming, Restrictions on Assigning to Variables for details.

Error in ==> externalprogram at 1
x=5
Error in ==> function1>function2 at 10
externalprogram

??? Error while evaluating uicontrol Callback

I understand that there is rule that does not allow variables to be added dynamically in a nested function, but there must be a way around this without copying and pasting the code from the external file inside the nested function. It is desirable to be able to cause another program to run inside the function, and a program isn't much good if it can't manipulate variables.

So how do I make this work?


I appreciate the help guys, and apologize for a long post or if I have asked a frequently asked question. I think once I understand these questions I will be much better off!

Subject: Function questions

From: Nasser Abbasi

Date: 9 May, 2009 07:27:57

Message: 2 of 9


"Johnathan " <durchfalldurchfall@yahoo.com> wrote in message
news:gu3a8b$pet$1@fred.mathworks.com...
> New member, so these questions have probably already been posed and
> answered.
>
> I have been making a GUI, and I have run into several hangups:
>
> 1) Accessing data from other functions:
>

... SNIP

Hi Johnathan,

I think the questions you are asking are answer in this good web page on
programming the GUI.

http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/&http://www.mathworks.com/support/product/product.html?product=ML

hope this helps.

--Nasser

Subject: Function questions

From: Johnathan

Date: 9 May, 2009 07:35:01

Message: 3 of 9

"Nasser Abbasi" <nma@12000.org> wrote in message <fmaNl.16319$pr6.1809@flpi149.ffdc.sbc.com>...

Nasser, unfortunately that link doesn't take me anywhere but the first page of the help manual.

I have read many help manual pages but have not figured it out, and if you have time I would really appreciate it if you could help me out more specifically as per the example I gave.

Thank you for the help!

Subject: Function questions

From: Bruno Luong

Date: 9 May, 2009 07:42:02

Message: 4 of 9

I keep this message for copy and past now and then:

Functions in GUI, like any other functions, have their own private workspace. They are not shared, and they are clear when the (nested) function ends.

To pass data among functions in Gui (it can be many guis), we have to use one of these options:

- GUIDATA
- SET/GETAPPDATA
- SET/GET on the field USERDATA of graphic handles of your GUI
- GLOBAL variables
- ASSIGNIN/EVALIN

The first ones are preferred methods. The two last are to be avoided.

Bruno

Subject: Function questions

From: Johnathan

Date: 9 May, 2009 08:52:01

Message: 5 of 9

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <gu3c4a$ob1$1@fred.mathworks.com>...
> I keep this message for copy and past now and then:
>
> Functions in GUI, like any other functions, have their own private workspace. They are not shared, and they are clear when the (nested) function ends.
>
> To pass data among functions in Gui (it can be many guis), we have to use one of these options:
>
> - GUIDATA
> - SET/GETAPPDATA
> - SET/GET on the field USERDATA of graphic handles of your GUI
> - GLOBAL variables
> - ASSIGNIN/EVALIN
>
> The first ones are preferred methods. The two last are to be avoided.
>
> Bruno

I see now how to transfer data with guihandles:

function function1
f=figure('Name', 'Thing', 'Position', [1,1,500,500]);

button1=uicontrol('Style', 'pushbutton', 'Callback',{@function2}, 'Position', [100,100,50,30]);

button2=uicontrol('Style', 'pushbutton', 'Position', [200,100,50,30], 'Callback',{@function3});

myhandles=guihandles(f);
myhandles.button1.string='boy';
guidata(f,myhandles);

function function2(object, eventdata)
myhandles=guidata(gcbo);
myhandles.button1.string
set(button1,'String',myhandles.button1.string)
% = gcbo returns the handle of the graphics object whose callback is executing.
%[h,figure] = gcbo returns the handle of the current callback object and the handle of the figure containing this object.
myhandles.button1.string='dog';
guidata(gcbo,myhandles);
end

function function3(object, eventdata)
myhandles=guidata(gcbo);
set(button1,'String',myhandles.button1.string)
myhandles.button1.string
end
end



when I press button 1:
ans =
boy
and button 1 now says boy

when I press button 2:
ans =
dog
and button 1 now says dog


Is it possible to create this type of structure which is accessible to multiple functions without using guihandles, as in from the struct data type? This only works when associated with a figure. If not, would it be advisable to use non-visible figures to pass data between functions in a non-GUI related program?

Also, what exactly is this doing? Is it saving your information to the temporary figure file and then reading it when you use guidata? Would it be a bad idea to use an external .txt or other file to pass information from program to program or function to function, or is there another way to do it? Is this common practice?

Thanks guys,
John

Subject: Function questions

From: Johnathan

Date: 9 May, 2009 09:03:01

Message: 6 of 9

also, I am still at a loss with the adding variables dynamically thing. If I nest the functions as per above, then I really need to figure that one out!

Subject: Function questions

From: Bruno Luong

Date: 9 May, 2009 09:46:01

Message: 7 of 9


>
>
> Is it possible to create this type of structure which is accessible to multiple functions without using guihandles, as in from the struct data type? This only works when associated with a figure. If not, would it be advisable to use non-visible figures to pass data between functions in a non-GUI related program?

Why such a need? Non GUI program are usually in the same thread, no callback or timer involved, linearly performed. So just create your structures and pass them among functions as input arguments. Function parameter is designed for that, abuse it.

>
> Also, what exactly is this doing? Is it saving your information to the temporary figure file and then reading it when you use guidata?

There is no file involved.

> Would it be a bad idea to use an external .txt or other file to pass information from program to program or function to function, or is there another way to do it? Is this common practice?

Yes it's a bad idea using file. Just use function parameters as commonly practiced. What is the fear of it??? Such strange question.

Bruno

Subject: Function questions

From: Loren Shure

Date: 11 May, 2009 11:55:35

Message: 8 of 9

In article <gu3c4a$ob1$1@fred.mathworks.com>,
b.luong@fogale.findmycountry says...
> I keep this message for copy and past now and then:
>
> Functions in GUI, like any other functions, have their own private workspace. They are not shared, and they are clear when the (nested) function ends.
>
> To pass data among functions in Gui (it can be many guis), we have to use one of these options:
>
> - GUIDATA
> - SET/GETAPPDATA
> - SET/GET on the field USERDATA of graphic handles of your GUI
> - GLOBAL variables
> - ASSIGNIN/EVALIN
>
> The first ones are preferred methods. The two last are to be avoided.
>
> Bruno
>

Another option: use nested functions.

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

Subject: Function questions

From: Bruno Luong

Date: 11 May, 2009 12:04:02

Message: 9 of 9

Loren Shure <loren@mathworks.com> wrote in message <MPG.2471dc3ebd35e04898999e@news.mathworks.com>...
> In article <gu3c4a$ob1$1@fred.mathworks.com>,
> b.luong@fogale.findmycountry says...
> > I keep this message for copy and past now and then:
> >
> > Functions in GUI, like any other functions, have their own private workspace. They are not shared, and they are clear when the (nested) function ends.
> >
> > To pass data among functions in Gui (it can be many guis), we have to use one of these options:
> >
> > - GUIDATA
> > - SET/GETAPPDATA
> > - SET/GET on the field USERDATA of graphic handles of your GUI
> > - GLOBAL variables
> > - ASSIGNIN/EVALIN
> >
> > The first ones are preferred methods. The two last are to be avoided.
> >
> > Bruno
> >
>
> Another option: use nested functions.
>

And to pass the nested function handle around, use... the above of course

Bruno

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
ge Bruno Luong 9 May, 2009 03:44:04
structure Johnathan 9 May, 2009 03:14:04
handles Johnathan 9 May, 2009 03:14:04
functions Johnathan 9 May, 2009 03:14:04
separate Johnathan 9 May, 2009 03:14:04
function Johnathan 9 May, 2009 03:14:04
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com