Passing data from Gui to seperate m.file

  • Follow


Hi,

I am new to MATLAB and I seem to have a problem using setappdata when passing
more then one piece of data from a gui to a seperate m.file, where calculations are then
performed on data passed from a GUI. The issue seems to be that the first
value passed from the GUI, say 'X,' is then overwritten with the next value,
say 'Y'. So in this simple example I have 

fh= figure (.....)
ftextX = uicontrol (...)  % Enter X value
ftexteditX = uicontrol (...)  % Edit box for user to enter X value

Then check if 'String' field is initially empty, if so set edit box to zero,
then
setappdata(0,'String', ftexteditX)  

% then repeat for Y, 

AdderPushbutton = uicontrol(.....'Callback', {@Adderftn}); % Call function to
add X and Y and return Z(the total of X + Y)

%Adderftn
function[varargout] = Adderftn(varargin)
ftexteditX = getappdata(0, 'String')
setappdata (0, 'String', ftexteditX)
X = str2num(get(ftexteditX, 'String'))

When I repeat for 'Y', the 'X' value is over written with 'Y'. However, if I remove the line 'setappdata(0,'String', ftexteditY)', both X and Y are now the value of 'X'. I can't seem
to spot the fundamental mistake I am obviously making. I don't any have
issues doing this using nested functions or passing data between functions
saved in seperate m.files, only I can't get the hang of doing the above.
Should I try passing the handle of the figure rather than root 0?

many thanks for any help you can give me
0
Reply Lawrence 10/12/2010 2:29:05 PM

"Lawrence Jones" <thejonesesplus1@gmail.com> wrote in message <i91rbh$2o1$1@fred.mathworks.com>...
> Hi,
> 
> I am new to MATLAB and I seem to have a problem using setappdata when passing
> more then one piece of data from a gui to a seperate m.file, where calculations are then
> performed on data passed from a GUI. The issue seems to be that the first
> value passed from the GUI, say 'X,' is then overwritten with the next value,
> say 'Y'. So in this simple example I have 
> 
> fh= figure (.....)
> ftextX = uicontrol (...)  % Enter X value
> ftexteditX = uicontrol (...)  % Edit box for user to enter X value
> 
> Then check if 'String' field is initially empty, if so set edit box to zero,
> then
> setappdata(0,'String', ftexteditX)  
> 
> % then repeat for Y, 
> 
> AdderPushbutton = uicontrol(.....'Callback', {@Adderftn}); % Call function to
> add X and Y and return Z(the total of X + Y)
> 
> %Adderftn
> function[varargout] = Adderftn(varargin)
> ftexteditX = getappdata(0, 'String')
> setappdata (0, 'String', ftexteditX)
> X = str2num(get(ftexteditX, 'String'))
> 
> When I repeat for 'Y', the 'X' value is over written with 'Y'. However, if I remove the line 'setappdata(0,'String', ftexteditY)', both X and Y are now the value of 'X'. I can't seem
> to spot the fundamental mistake I am obviously making. I don't any have
> issues doing this using nested functions or passing data between functions
> saved in seperate m.files, only I can't get the hang of doing the above.
> Should I try passing the handle of the figure rather than root 0?
> 
> many thanks for any help you can give me


I'm not sure about what you're doing exactly but try this
fhx = figure(...); 
....follow xdata information
 ftexteditX = getappdata(fhx,'String');

fhy = figure(...); 
....follow ydata information
 ftexteditY = getappdata(fhy,'String');

by not specifying the figure handle you may have been causing yourself issues.
0
Reply Joseph 10/12/2010 3:50:06 PM



"Lawrence Jones" <thejonesesplus1@gmail.com> wrote in message 
news:i91rbh$2o1$1@fred.mathworks.com...
> Hi,
>
> I am new to MATLAB and I seem to have a problem using setappdata when 
> passing
> more then one piece of data from a gui to a seperate m.file, where 
> calculations are then
> performed on data passed from a GUI. The issue seems to be that the first
> value passed from the GUI, say 'X,' is then overwritten with the next 
> value,
> say 'Y'. So in this simple example I have
> fh= figure (.....)
> ftextX = uicontrol (...)  % Enter X value
> ftexteditX = uicontrol (...)  % Edit box for user to enter X value
>
> Then check if 'String' field is initially empty, if so set edit box to 
> zero,
> then
> setappdata(0,'String', ftexteditX)
> % then repeat for Y,
> AdderPushbutton = uicontrol(.....'Callback', {@Adderftn}); % Call function 
> to
> add X and Y and return Z(the total of X + Y)
>
> %Adderftn
> function[varargout] = Adderftn(varargin)
> ftexteditX = getappdata(0, 'String')
> setappdata (0, 'String', ftexteditX)
> X = str2num(get(ftexteditX, 'String'))
>
> When I repeat for 'Y', the 'X' value is over written with 'Y'.

That's correct.

setappdata(0, 'String', myXinformation)
setappdata(0, 'String', myYinformation)

will set the application data named 'String' first to myXinformation then 
overwrite that application data to myYinformation.

> However, if I remove the line 'setappdata(0,'String', ftexteditY)', both X 
> and Y are now the value of 'X'. I can't seem
> to spot the fundamental mistake I am obviously making.

Don't use the same name for two different things, or if you must use a 
container that can hold multiple different things as the value associated 
with that name.

clear S
S.x = myXinformation;
S.y = myYinformation;
setappdata(0, 'String', S)

What I would do is to use one of the other techniques for sharing data among 
GUI components:

http://www.mathworks.com/help/techdoc/creating_guis/f5-998197.html
http://www.mathworks.com/help/techdoc/creating_guis/f13-998197.html

> I don't any have
> issues doing this using nested functions or passing data between functions
> saved in seperate m.files, only I can't get the hang of doing the above.
> Should I try passing the handle of the figure rather than root 0?

Yes, but that won't prevent the above problem from happening.  [It would 
prevent the problem of some OTHER GUI deciding to set application data with 
name 'String' in the root object and overwriting your GUI's data.]

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com 

0
Reply slord (13285) 10/12/2010 5:05:42 PM

Hi Steve,

Many thanks for your help. I have changed the variable 'String' to 'InputX' and 'InputY'. I was assuming that you had to use 'String' to read the values entered in the X and Y edit boxes. I have specified my figure, however X and Y are entered on the same and only figure, which is the main gui saved as myfun.m. The Adderftn is then called, and my aim is to get the adderftn (saved in adderftn.m) to read the X and Y fields, and then add them together to get Z. Z is then read by myfun.m and diplayed in the gui as the answer.

 My code at the moment now looks like this:
function myfun()
f1 = figure(....)
ft = uicontrol(f1,....)   % Static text box displaying 'Enter X value here'
fted = uicontrol(f1,....)   % Empty edit text box for user entered X value
input = str2num(get(fted, 'String'));
if (isempty(input))
     set (fted, 'String', '0')  % in order to disply '0' in the edit box for 'X'
end
fstringX = get(fted, 'String')  % Sets fstringX to edit box value of '0'
setappdata(0, 'InputX', fstringX) 

I then repeat for 'Y' (except the figure line) ending with setappdata(0, 'InputY', fstringY), then;
Push_button_1 = uicontrol(f1,...., 'Callback', {@Adderftn}); %Adderftn stored in a separate m.file
function [varargout] = Adderftn(varargin)
fstringX = getappdata(0, 'InputX')
setappdata(0, 'InputX', fStringX)
X= fstringX   % and repeat for reading the 'Y' value
total = X + Y
setappdata(0, 'Z', total)

As you can probably see, the problem I have now is that InputX and InputY now only read the inital values, i.e '0'. Have I just written this all in the wrong way? Ultimately I would like to do something more involved with this technique, like make a selection in a gui and then call the m.file containing matrix, and extract the relevant selection from that matrix for processing subsequent functions. Many thanks again for your help
0
Reply Lawrence 10/13/2010 2:01:05 PM


"Lawrence Jones" <thejonesesplus1@gmail.com> wrote in message 
news:i94e31$h8p$1@fred.mathworks.com...
> Hi Steve,
>
> Many thanks for your help. I have changed the variable 'String' to 
> 'InputX' and 'InputY'. I was assuming that you had to use 'String' to read 
> the values entered in the X and Y edit boxes.

You were mixing the property name of your UICONTROL and the application data 
name.

> I have specified my figure, however X and Y are entered on the same and 
> only figure, which is the main gui saved as myfun.m. The Adderftn is then 
> called, and my aim is to get the adderftn (saved in adderftn.m) to read 
> the X and Y fields, and then add them together to get Z. Z is then read by 
> myfun.m and diplayed in the gui as the answer.

Then why are you using the application data?  Just have your adderftn (which 
I assume has [or can have] access to the GUI handles) retrieve the 
properties from the uicontrols using the handles structure, add them, and 
update the appropriate element in the GUI.

*snip*

> As you can probably see, the problem I have now is that InputX and InputY 
> now only read the inital values, i.e '0'. Have I just written this all in 
> the wrong way? Ultimately I would like to do something more involved with 
> this technique, like make a selection in a gui and then call the m.file 
> containing matrix, and extract the relevant selection from that matrix for 
> processing subsequent functions. Many thanks again for your help

No offense intended, but to be perfectly honest I think you're overthinking 
the problem.  From your description, there doesn't seem to be any need to go 
through the application data (with all the potential issues that raises) 
rather than giving the "workhorse" function direct access to the handles (or 
writing an interface function that retrieves the properties, calls the 
workhorse with the appropriate inputs, receives the workhorse's outputs, and 
updates the GUI proper, if you don't want the workhorse function to have 
that much visibility into the GUI.)

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com 

0
Reply Steven_Lord 10/13/2010 5:44:47 PM

4 Replies
509 Views

(page loaded in 0.076 seconds)

Similiar Articles:













7/26/2012 10:47:04 AM


Reply: