<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250945</link>
    <title>MATLAB Central Newsreader - Function questions</title>
    <description>Feed for thread: Function questions</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sat, 09 May 2009 07:10:03 -0400</pubDate>
      <title>Function questions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250945#648558</link>
      <author>Johnathan </author>
      <description>New member, so these questions have probably already been posed and answered.&lt;br&gt;
&lt;br&gt;
I have been making a GUI, and I have run into several hangups:&lt;br&gt;
&lt;br&gt;
1) Accessing data from other functions:&lt;br&gt;
&lt;br&gt;
If i wrote a file like this:&lt;br&gt;
&lt;br&gt;
%%%%%%%%%%%%%%%&lt;br&gt;
&lt;br&gt;
function function1&lt;br&gt;
f=figure('Name', 'Thing', 'Position', [1,1,500,500])&lt;br&gt;
button1=uicontrol('Style', 'pushbutton', 'Callback',{@function2}, 'Position', [100,100,50,30])&lt;br&gt;
&lt;br&gt;
button2=uicontrol('Style', 'pushbutton', 'Position', [200,100,50,30])&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
function function2(object, eventdata)&lt;br&gt;
get(button1, 'Value')&lt;br&gt;
externalprogram&lt;br&gt;
&lt;br&gt;
%%%%%%%%%%%%%%%%&lt;br&gt;
&lt;br&gt;
where externalprogram is an m-file like:&lt;br&gt;
&lt;br&gt;
%%%%%%%%%%%%%%&lt;br&gt;
x=5&lt;br&gt;
%%%%%%%%%%%%%%&lt;br&gt;
&lt;br&gt;
Then I get an error &lt;br&gt;
&lt;br&gt;
??? Undefined function or variable 'button1'.&lt;br&gt;
&lt;br&gt;
Error in ==&amp;gt; function1&amp;gt;function2 at 9&lt;br&gt;
get(button1, 'Value')&lt;br&gt;
??? Error while evaluating uicontrol Callback&lt;br&gt;
&lt;br&gt;
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?&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
2) If you close the functions as per above with &quot;end&quot;s and nest them, you can say get(button1,'Value'), but when externalprogram tries to run you get a different error:&lt;br&gt;
&lt;br&gt;
??? Attempt to add &quot;x&quot; to a static workspace.&lt;br&gt;
&amp;nbsp;See MATLAB Programming, Restrictions on Assigning to Variables for details.&lt;br&gt;
&lt;br&gt;
??? Attempt to add &quot;x&quot; to a static workspace.&lt;br&gt;
&amp;nbsp;See MATLAB Programming, Restrictions on Assigning to Variables for details.&lt;br&gt;
&lt;br&gt;
Error in ==&amp;gt; externalprogram at 1&lt;br&gt;
x=5&lt;br&gt;
Error in ==&amp;gt; function1&amp;gt;function2 at 10&lt;br&gt;
externalprogram&lt;br&gt;
&lt;br&gt;
??? Error while evaluating uicontrol Callback&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
So how do I make this work?&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
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!</description>
    </item>
    <item>
      <pubDate>Sat, 09 May 2009 07:27:57 -0400</pubDate>
      <title>Re: Function questions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250945#648564</link>
      <author>Nasser Abbasi</author>
      <description>&lt;br&gt;
&quot;Johnathan &quot; &amp;lt;durchfalldurchfall@yahoo.com&amp;gt; wrote in message &lt;br&gt;
news:gu3a8b$pet$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; New member, so these questions have probably already been posed and &lt;br&gt;
&amp;gt; answered.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I have been making a GUI, and I have run into several hangups:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; 1) Accessing data from other functions:&lt;br&gt;
&amp;gt;&lt;br&gt;
&lt;br&gt;
... SNIP&lt;br&gt;
&lt;br&gt;
Hi Johnathan,&lt;br&gt;
&lt;br&gt;
I think the questions you are asking are answer in this good web page on &lt;br&gt;
programming the GUI.&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/&amp;http://www.mathworks.com/support/product/product.html?product=ML&quot;&gt;http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/&amp;http://www.mathworks.com/support/product/product.html?product=ML&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
hope this helps.&lt;br&gt;
&lt;br&gt;
--Nasser</description>
    </item>
    <item>
      <pubDate>Sat, 09 May 2009 07:35:01 -0400</pubDate>
      <title>Re: Function questions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250945#648566</link>
      <author>Johnathan </author>
      <description>&quot;Nasser Abbasi&quot; &amp;lt;nma@12000.org&amp;gt; wrote in message &amp;lt;fmaNl.16319$pr6.1809@flpi149.ffdc.sbc.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
Nasser, unfortunately that link doesn't take me anywhere but the first page of the help manual. &lt;br&gt;
&lt;br&gt;
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. &lt;br&gt;
&lt;br&gt;
Thank you for the help!</description>
    </item>
    <item>
      <pubDate>Sat, 09 May 2009 07:42:02 -0400</pubDate>
      <title>Re: Function questions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250945#648569</link>
      <author>Bruno Luong</author>
      <description>I keep this message for copy and past now and then:&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
To pass data among functions in Gui (it can be many guis), we have to use one of these options:&lt;br&gt;
&lt;br&gt;
- GUIDATA&lt;br&gt;
- SET/GETAPPDATA&lt;br&gt;
- SET/GET on the field USERDATA of graphic handles of your GUI&lt;br&gt;
- GLOBAL variables&lt;br&gt;
- ASSIGNIN/EVALIN&lt;br&gt;
&lt;br&gt;
The first ones are preferred methods. The two last are to be avoided.&lt;br&gt;
&lt;br&gt;
Bruno </description>
    </item>
    <item>
      <pubDate>Sat, 09 May 2009 08:52:01 -0400</pubDate>
      <title>Re: Function questions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250945#648580</link>
      <author>Johnathan </author>
      <description>&quot;Bruno Luong&quot; &amp;lt;b.luong@fogale.findmycountry&amp;gt; wrote in message &amp;lt;gu3c4a$ob1$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; I keep this message for copy and past now and then:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; To pass data among functions in Gui (it can be many guis), we have to use one of these options:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; - GUIDATA&lt;br&gt;
&amp;gt; - SET/GETAPPDATA&lt;br&gt;
&amp;gt; - SET/GET on the field USERDATA of graphic handles of your GUI&lt;br&gt;
&amp;gt; - GLOBAL variables&lt;br&gt;
&amp;gt; - ASSIGNIN/EVALIN&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The first ones are preferred methods. The two last are to be avoided.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno &lt;br&gt;
&lt;br&gt;
I see now how to transfer data with guihandles:&lt;br&gt;
&lt;br&gt;
function function1&lt;br&gt;
f=figure('Name', 'Thing', 'Position', [1,1,500,500]);&lt;br&gt;
&lt;br&gt;
button1=uicontrol('Style', 'pushbutton', 'Callback',{@function2}, 'Position', [100,100,50,30]);&lt;br&gt;
&lt;br&gt;
button2=uicontrol('Style', 'pushbutton', 'Position', [200,100,50,30], 'Callback',{@function3});&lt;br&gt;
&lt;br&gt;
myhandles=guihandles(f);&lt;br&gt;
myhandles.button1.string='boy';&lt;br&gt;
guidata(f,myhandles);&lt;br&gt;
&lt;br&gt;
function function2(object, eventdata)&lt;br&gt;
myhandles=guidata(gcbo);&lt;br&gt;
myhandles.button1.string&lt;br&gt;
set(button1,'String',myhandles.button1.string)&lt;br&gt;
% = gcbo returns the handle of the graphics object whose callback is executing.&lt;br&gt;
%[h,figure] = gcbo returns the handle of the current callback object and the handle of the figure containing this object.&lt;br&gt;
myhandles.button1.string='dog';&lt;br&gt;
guidata(gcbo,myhandles);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
function function3(object, eventdata)&lt;br&gt;
myhandles=guidata(gcbo);&lt;br&gt;
set(button1,'String',myhandles.button1.string)&lt;br&gt;
myhandles.button1.string&lt;br&gt;
end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
when I press button 1:&lt;br&gt;
ans =&lt;br&gt;
boy&lt;br&gt;
and button 1 now says boy&lt;br&gt;
&lt;br&gt;
when I press button 2:&lt;br&gt;
ans =&lt;br&gt;
dog&lt;br&gt;
and button 1 now says dog&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
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?&lt;br&gt;
&lt;br&gt;
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?&lt;br&gt;
&lt;br&gt;
Thanks guys,&lt;br&gt;
John</description>
    </item>
    <item>
      <pubDate>Sat, 09 May 2009 09:03:01 -0400</pubDate>
      <title>Re: Function questions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250945#648582</link>
      <author>Johnathan </author>
      <description>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!</description>
    </item>
    <item>
      <pubDate>Sat, 09 May 2009 09:46:01 -0400</pubDate>
      <title>Re: Function questions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250945#648586</link>
      <author>Bruno Luong</author>
      <description>&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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?&lt;br&gt;
&lt;br&gt;
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.&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Also, what exactly is this doing? Is it saving your information to the temporary figure file and then reading it when you use guidata? &lt;br&gt;
&lt;br&gt;
There is no file involved.&lt;br&gt;
&lt;br&gt;
&amp;gt; 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?&lt;br&gt;
&lt;br&gt;
Yes it's a bad idea using file. Just use function parameters as commonly practiced. What is the fear of it??? Such strange question.&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
    <item>
      <pubDate>Mon, 11 May 2009 11:55:35 -0400</pubDate>
      <title>Re: Function questions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250945#648903</link>
      <author>Loren Shure</author>
      <description>In article &amp;lt;gu3c4a$ob1$1@fred.mathworks.com&amp;gt;, &lt;br&gt;
b.luong@fogale.findmycountry says...&lt;br&gt;
&amp;gt; I keep this message for copy and past now and then:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; 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.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; To pass data among functions in Gui (it can be many guis), we have to use one of these options:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; - GUIDATA&lt;br&gt;
&amp;gt; - SET/GETAPPDATA&lt;br&gt;
&amp;gt; - SET/GET on the field USERDATA of graphic handles of your GUI&lt;br&gt;
&amp;gt; - GLOBAL variables&lt;br&gt;
&amp;gt; - ASSIGNIN/EVALIN&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; The first ones are preferred methods. The two last are to be avoided.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Bruno &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Another option: use nested functions.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Loren&lt;br&gt;
&lt;a href=&quot;http://blogs.mathworks.com/loren&quot;&gt;http://blogs.mathworks.com/loren&lt;/a&gt;</description>
    </item>
    <item>
      <pubDate>Mon, 11 May 2009 12:04:02 -0400</pubDate>
      <title>Re: Function questions</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/250945#648906</link>
      <author>Bruno Luong</author>
      <description>Loren Shure &amp;lt;loren@mathworks.com&amp;gt; wrote in message &amp;lt;MPG.2471dc3ebd35e04898999e@news.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; In article &amp;lt;gu3c4a$ob1$1@fred.mathworks.com&amp;gt;, &lt;br&gt;
&amp;gt; b.luong@fogale.findmycountry says...&lt;br&gt;
&amp;gt; &amp;gt; I keep this message for copy and past now and then:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; 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.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; To pass data among functions in Gui (it can be many guis), we have to use one of these options:&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; - GUIDATA&lt;br&gt;
&amp;gt; &amp;gt; - SET/GETAPPDATA&lt;br&gt;
&amp;gt; &amp;gt; - SET/GET on the field USERDATA of graphic handles of your GUI&lt;br&gt;
&amp;gt; &amp;gt; - GLOBAL variables&lt;br&gt;
&amp;gt; &amp;gt; - ASSIGNIN/EVALIN&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The first ones are preferred methods. The two last are to be avoided.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Bruno &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Another option: use nested functions.&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
And to pass the nested function handle around, use... the above of course&lt;br&gt;
&lt;br&gt;
Bruno</description>
    </item>
  </channel>
</rss>

