Path: news.mathworks.com!not-for-mail
From: "Johnathan " <durchfalldurchfall@yahoo.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Function questions
Date: Sat, 9 May 2009 08:52:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 66
Message-ID: <gu3g7h$gpr$1@fred.mathworks.com>
References: <gu3a8b$pet$1@fred.mathworks.com> <gu3c4a$ob1$1@fred.mathworks.com>
Reply-To: "Johnathan " <durchfalldurchfall@yahoo.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1241859121 17211 172.30.248.35 (9 May 2009 08:52:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 9 May 2009 08:52:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1099390
Xref: news.mathworks.com comp.soft-sys.matlab:538636


"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