You can use the figure KeyPressFcn callback to intercept the keyboard and output asterisks when the user types. Here are step by step instructions to implement an example that shows asterisks in the edit box as the user types and shows what the user typed in the static text box.
1) Start a new gui (Matlab window: File, New, Gui)
2) Add a edit text block (where the user will see asterisks)
3) Double click on the edit text block and change the property String from "Edit Text" to "Enter Password"
3) Add a static text block (where the actual typing will show up)
4) Right click somewhere in the gui figure (not on the edit text block or the static text block), select "View Callbacks", and select "KeyPressFcn". Save the file as test2.fig.
5) Enter the following under
function varargout = figure1_KeyPressFcn(h, eventdata, handles, varargin)
% Capture the key pressed
cc=get(handles.figure1,'CurrentCharacter');
% Get the current string in the edit and static text boxes
str1=get(handles.edit1,'string');
str2=get(handles.text1,'string');
% What is in the edit box
if (strcmp(str1,'Enter Password')==1)
% Edit box has 'Enter Password'
% This letter is the start of the password
str1 = '*';
str2 = cc;
else
% Edit box has something besides 'Enter Password'
% This letter is the next letter in the password
str1 = strcat(str1,'*');
str2 = strcat(str2,cc);
end
% Update the strings
set(handles.edit1,'string',str1);
set(handles.text1,'string',str2);
6) Save the files
7) Run the figure
While the example illustrates the technique, you will probably have to upgrade some things to have a practical, functional code. Typing anywhere in the figure except in the edit box will work as desired. Typing in the edit box still shows up as text. One fix is to just change the edit box enable property to inactive. An alternate fix would be to use a static box instead of an edit box. Since typing anywhere in the figure shows up in the password, a modal figure should be used and the password entry area should dominate that figure.
Also, as implemented, the program echos an asterisk for each character of the password. You could use a random function (or something else) to have just some of the asterisks appear.
If you need additional guidance, let me know.
Cheers
J. Vorwald