Re: Create GUI instances in existing figure
- From: "Jeremy Smith" <smit1729@xxxxxxxxxxxxxx>
- Date: Wed, 21 Jun 2006 11:58:24 -0400
You would actually be surprised at how easy it is to create a GUI
without GUIDE. It's even simpler in my opinion. GUIDE generates a
ton of code which you must sort through and may never use. Keep in
mind you're trying to do some more advanced GUI actions so it may be
better to take a bit more time to learn Matlab GUIs a bit more in
depth.
Some commands you'll find useful: set, get, setappdata, getappdata,
evalin, assignin, uicontrol, uimenu, uicontextmenu, dialog, figure,
and figflag.
When you are dealing with callbacks you must use function handles and
remember that when you have a callback linked to a GUI object it
sends two extra bits of info. The first is the calling object's
handle and the second is eventdata (which is currently not used).
What follows is a simple GUI I made as a login with an asterix hidden
password.
% logindlg.m
% [Login Password] = logindlg(Title)
%
% Author: Jeremy Smith
% Date: September 24, 2005
% Last Edit: June 19, 2006
% Version: 1.1
% Tested on: Matlab 7.0.4.365 (R14) Service Pack 2
% Description: custom login dialog because Matlab doesn't have an
option
% for characters in an edit field to be replaced by asterixes
% (password security)
% To Do:
% Correct OK function to allow tab switching of active object
% Changelist:
% 1.2: -Removed horizontal alignment from buttons
% 1.1: -Added positioning code so it'll display in the center of
the screen
% -If only one output is specified the password will be
returned
% instead of the login as in Version 1.0
% -Escape will not only close the dialog if neither edit box
is active
% -When the dialog appears the first edit box will be active
% -Added a few more comments
% -Removed the clc, it was left in by mistake in Version 1.0
function [varargout]=logindlg(Title)
% Input Error Check
if nargin < 1 || nargin == 1 && ischar(Title) == 0
Title = 'Login';
end
% Output Error Check
if nargout > 2
error('Too many output arguments.')
end
% Get Properties
Color = get(0,'DefaultUicontrolBackgroundcolor');
% Determine the size and position of the login interface
set(0,'Units','characters')
Screen = get(0,'screensize');
Position = [Screen(3)/2-17.5 Screen(4)/2-4.75 35 9.5];
set(0,'Units','pixels')
% Create the GUI
gui.main = dialog('HandleVisibility','on',...
'IntegerHandle','off',...
'Menubar','none',...
'NumberTitle','off',...
'Name','Login',...
'Tag','logindlg',...
'Color',Color,...
'Units','characters',...
'Userdata','logindlg',...
'Position',Position);
% Set the title
if ischar(Title) == 1
set(gui.main,'Name',Title,'Closerequestfcn',{@Cancel,gui.main},'Keypre
ssfcn',{@Escape,gui.main})
end
% Texts
gui.login_text =
uicontrol(gui.main,'Style','text','FontSize',8,'HorizontalAlign','left
','Units','characters','String','Login','Position',[1 7.65 20 1]);
gui.password_text =
uicontrol(gui.main,'Style','text','FontSize',8,'HorizontalAlign','left
','Units','characters','String','Password','Position',[1 4.15 20 1]);
% Edits
gui.edit1 =
uicontrol(gui.main,'Style','edit','FontSize',8,'HorizontalAlign','left
','BackgroundColor','white','Units','characters','String','','Position
',[1 6.02 33 1.7]);
gui.edit2 =
uicontrol(gui.main,'Style','edit','FontSize',8,'HorizontalAlign','left
','BackgroundColor','white','Units','characters','String','','Position
',[1 2.52 33
1.7],'Callback',{@OK,gui.main},'KeyPressfcn',{@KeyPress_Function},'Use
rdata','');
% Buttons
gui.OK =
uicontrol(gui.main,'Style','push','FontSize',8,'Units','characters','S
tring','OK','Position',[12 .2 10 1.7],'Callback',{@OK,gui.main});
gui.Cancel =
uicontrol(gui.main,'Style','push','FontSize',8,'Units','characters','S
tring','Cancel','Position',[23 .2 10
1.7],'Callback',{@Cancel,gui.main});
setappdata(0,'logindlg',gui) % Save handle data
setappdata(gui.main,'Check',0) % Error check setup. If Check remains
0 an empty cell array will be returned
uicontrol(gui.edit1) % Make the first edit box active
% Pause the GUI and wait for a button to be pressed
uiwait(gui.main)
Check = getappdata(gui.main,'Check'); % Check to see if a button was
pressed
% Format output
if Check == 1
Login = get(gui.edit1,'String');
Password = get(gui.edit2,'Userdata');
if nargout == 1 % If only one output specified output Password
varargout(1) = {Login};
elseif nargout == 2 % If two outputs specified output both Login
and Password
varargout(1) = {Login};
varargout(2) = {Password};
end
else % If OK wasn't pressed output nothing
if nargout == 1
varargout(1) = {[]};
elseif nargout == 2
varargout(1) = {[]};
varargout(2) = {[]};
end
end
delete(gui.main) % Close the GUI
setappdata(0,'logindlg',[]) % Erase handles from memory
%% Hide Password
function KeyPress_Function(h,eventdata)
% Function to replace all characters in the password edit box with
% asterixes
password = get(h,'Userdata');
key = get(gcf,'currentkey');
if isempty(strfind(key,'backspace')) == 0
password = password(1:end-1); % Delete the last character in the
password
else
password = [password get(gcf,'currentcharacter')]; % Add the
typed character to the password
end
ch = get(gcf,'currentcharacter'); % Get the typed character
SizePass = size(password); % Find the number of asterixes
if SizePass(2) > 0
asterix(1,1:SizePass(2)) = '*'; % Create a string of asterixes
the same size as the password
set(h,'String',asterix) % Set the text in the password edit box
to the asterix string
else
set(h,'String','')
end
set(h,'Userdata',password) % Store the password in its current state
%% Cancel
function Cancel(h,eventdata,fig)
uiresume(fig)
%% OK
function OK(h,eventdata,fig)
% Set the check and resume
setappdata(fig,'Check',1)
uiresume(fig)
%% Escape
function Escape(h,eventdata,fig)
% Close the login if the escape button is pushed and neither edit box
is
% active
key = get(fig,'currentkey');
if isempty(strfind(key,'escape')) == 0 && h == fig
Cancel([],[],fig)
end
.
- Follow-Ups:
- Re: Create GUI instances in existing figure
- From: Frank Wuebbeling
- Re: Create GUI instances in existing figure
- References:
- Create GUI instances in existing figure
- From: Frank Wuebbeling
- Re: Create GUI instances in existing figure
- From: Jeremy Smith
- Re: Create GUI instances in existing figure
- From: Frank Wuebbeling
- Create GUI instances in existing figure
- Prev by Date: Re: text always overlaying patches?
- Next by Date: Possible to read text off an image?
- Previous by thread: Re: Create GUI instances in existing figure
- Next by thread: Re: Create GUI instances in existing figure
- Index(es):