MATLAB GUI Simple example : Text edit

[code language=”css”]
function mytext(action)

if nargin<1
action=’initialize’;
%execute mytext(initialize) when user enter mytext in command window
end;

if strcmp(action,’initialize’),

figure(‘Name’,’Example TextEdit’,…
‘NumberTitle’,’off’,…
‘Units’,’pixels’,…
‘Position’,[500 500 400 100],…
‘MenuBar’,’none’);

medit=uicontrol(‘Style’,’edit’,…
‘Units’,’normalized’, …
‘Position’,[0.1 0.05 0.8 0.4],…
‘Callback’,’mytext(”update”)’);
%execute command mytext(update) when user press enter
%from texteditor

lbl=uicontrol(‘Style’,’text’,…
‘Units’,’normalized’, …
‘Position’,[0.1 0.5 0.8 0.4],…
‘String’,’Hello’);

hndlList=[medit lbl];
set(gcf,’UserData’,hndlList); %save texteditor and label to current graphic
% texteditor and label is saved into UserData
end;

if(strcmp(action,’update’))
hndlList=get(gcf,’UserData’);
%get texteditor and label from current graphic
%texteditor and label is loaded from UserData

medit=hndlList(1); %get text editor from current graphic
str=get(medit,’String’) % get string from text editor

lbl=hndlList(2); %get label from current graphic
set(lbl,’String’,str); %set text to label
end
[/code]