Path: news.mathworks.com!not-for-mail
From: "Johnathan " <durchfalldurchfall@yahoo.com>
Newsgroups: comp.soft-sys.matlab
Subject: Function questions
Date: Sat, 9 May 2009 07:10:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 61
Message-ID: <gu3a8b$pet$1@fred.mathworks.com>
Reply-To: "Johnathan " <durchfalldurchfall@yahoo.com>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1241853003 26077 172.30.248.38 (9 May 2009 07:10:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 9 May 2009 07:10:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1099390
Xref: news.mathworks.com comp.soft-sys.matlab:538614


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!