Stephanie Chung:
<SNIP wants to enter data in an edit box>
from a recent posting:
this is a slightly enhanced way of doing it - and it well may be an
overkill for your app.
for educational purposes, it employs two types of data containers.
1) save the code snippet below in a file, eg, <getdata.m>.
2) at the command prompt, type
ic;
3) two controls are created:
the top one is your edit box
4) enter any valid ML command that creates
a number, eg,
12/10
sin(30*pi/180)
the accumulated numbers will show up
in the listbox below
5) if your finished, type two returns
in your edit box
6) the accumulator will be cleared
7) retrieve your currend data set via
d=getappdata(gcf,'data');
in another program or the command window
%<<< CODE >>>
function getdata(uh)
if ~nargin
% create controls
p.txt='enter data into edit box above';
p.uh(1)=uicontrol('position',...
[10 300 200 20]);
set(p.uh(1),'style','edit');
set(p.uh(1),'horizontalalignment',...
'left');
set(p.uh(1),'callback',...
[mfilename '(gcbo);']);
p.uh(2)=uicontrol('position',...
[10 10 200 285]);
set(p.uh(2),'style','listbox');
set(p.uh(2),'max',1);
set(p.uh(2),'string',p.txt);
setappdata(p.uh(1),'handles',p);
else
% accumulate data until user enters
% two successive returns (== empty string)
p=getappdata(uh,'handles');
s=get(uh,'string');
set(uh,'string',[]);
if ~isempty(s)
try
% evaluate input like sin(30*pi/180)
s=eval(s);
d=[get(p.uh(1),'userdata');s];
set(p.uh(1),'userdata',d);
set(p.uh(2),'string',d);
set(p.uh(2),'value',length(d));
catch
% launch a warning
% here: do nothing
end
else
% store data in <gcf>
% clear accumulator
d=get(p.uh(1),'userdata');
setappdata(gcf,'data',d);
set(p.uh(1),'userdata',[]);
set(p.uh(2),'value',1);
set(p.uh(2),'string',p.txt);
end
end
return;
|