Last week I explained how Matlab’s editbox control can be customized using its underlying Java component. Today I extend that article by explaining how we can use this information to provide a very user-friendly input-validation function for Matlab editboxes.

Zip-code entry validation example
As before, we first need to get the Matlab control’s underlying Java control. This is done using the findjobj utility:
% Prepare the log editbox hEditbox = uicontrol('Style','edit', 'String','Matlab', ...); % Get the underlying Java editbox jEditbox = findjobj(hLogPanel); try % Multi-line editboxes are contained within a scroll-panel jEditbox = handle(jEditbox.getViewport.getView, 'CallbackProperties'); catch % probably a single-line editbox end
Callbacks
Once we have the jEditbox
reference handle, we can use some ~30 different event callbacks that it exposes. Some of the useful callbacks include:
- ActionPerformedCallback – fired when
is clicked in the editbox - CaretUpdateCallback – fired when the caret position has changed
- KeyTypedCallback – fired whenever a keyboard button is typed when the editbox has focus
For example:
set(jEditbox, 'KeyPressedCallback',@myValidationFunction); % Callback function definition function myValidationFunction(jEditbox, eventData) % The following comments refer to the case of Alt-Shift-b keyChar = get(eventData,'KeyChar'); % or: eventData.getKeyChar ==> 'B' isAltDown = get(eventData,'AltDown'); % ==> 'on' isAltDown = eventData.isAltDown; % ==> true modifiers = get(eventData,'Modifiers'); % or: eventData.getModifiers ==> 9 = 1 (Shift) + 8 (Alt) modifiersDescription = char(eventData.getKeyModifiersText(modifiers)); % ==> 'Alt+Shift' % (now decide what to do with this key-press...) end
Using such a validation function enables us to immediately convert typed characters into ‘*’ (in password fields) or check that the input conforms to a standard format such as a 5-digit zip code, a valid-looking email address or a valid-looking credit-card number (Luhn’s algorithm, recently featured in a video tutorial by Doug Hull). Of course, there are dedicated JIDE controls that do much of this already, but let’s not digress.
In today’s example, we shall implement a simple input-validation function for a 5-digit zip code. When the input data is invalid, the editbox will be colored red and an appropriate message will appear next to the editbox. In addition, invalid (non-digit) characters shall be rejected with a beep sound.
Zip-code validation example
To illustrate the above, let’s use an example of a 5-digit zip-code entry validation. We start by creating an empty editbox with an associated message label next to it:
figure('Color','w', 'Menubar','none'); hEditbox = uicontrol('Style','edit', 'Pos',[10,10,60,20], 'String','Matlab'); % start with an invalid string hMessageLabel = uicontrol('Style','text', 'Pos',[80,10,200,20], 'horizontal','left', 'background','w', 'Foreground','red');
Next we get the underlying jEditbox
reference and set its entry-validation callback function:
jEditbox = findjobj(hEditbox); % single-line editbox so there's need to drill-down the scroll-pane set(jEditbox, 'KeyPressedCallback',{@editboxValidation,hMessageLabel});
Note how we pass the message-label’s handle as an extra input parameter to the callback function.
Now we define the callback function editboxValidation and place it on the Matlab path (for example, by placing the following within editboxValidation.m in a folder that is already on your path):
% editboxValidation - ensure that an editbox input is a valid 5-digit zip code function editboxValidation(jEditbox, eventData, hMessageLabel) keyChar = eventData.getKeyChar; % see note below about how we can trick Matlab here if ~alldigits(keyChar) && isprintable(keyChar) beep; fixedText = strrep(char(jEditbox.getText), keyChar, ''); jEditbox.setText(fixedText); end updateAppearance(jEditbox, hMessageLabel); end % Check whether a string only contains digits function flag = alldigits(str) flag = ~isnan(str2double(str)); end % Check whether a character is printable function flag = isprintable(keyChar) keyVal = double(keyChar); flag = ~isempty(keyVal) && keyVal > 31 && keyVal < 128; end % Update the GUI appearance based on the editbox text function updateAppearance(jEditbox, hMessageLabel) currentText = char(jEditbox.getText); if isempty(currentText) set(hMessageLabel, 'String','Please enter a 5-digit zip-code') jEditbox.setBorder(javax.swing.border.LineBorder(java.awt.Color.red, 3, false)); elseif ~alldigits(currentText) beep; set(hMessageLabel, 'String','Invalid zip: should only contain digits') jEditbox.setBorder(javax.swing.border.LineBorder(java.awt.Color.red, 3, false)); elseif length(currentText) ~= 5 set(hMessageLabel, 'String','Invalid zip: should have exactly 5 digits') jEditbox.setBorder(javax.swing.border.LineBorder(java.awt.Color.red, 3, false)); else set(hMessageLabel, 'String',''); jEditbox.setBorder(javax.swing.border.LineBorder(java.awt.Color.gray, 1, false)); end end
Finally, we call the validation function directly after creating our GUI, to let the user know that the zip-code needs to be entered:
% Note how we trick Matlab by using eventData as a struct rather than a Java object % (i.e., getKeyChar is set here to be a simple struct field, rather than a Java method) eventData.getKeyChar = ''; editboxValidation(jEditbox,eventData,hMessageLabel)

Zip-code entry validation example
Naturally, this is a very simple example, and you can easily expand it for your needs. But it does show the basic components of any input validation: checking the new data, updating the control as appropriate, and modifying the appearance of the underlying control and of other associated controls.
Available report - "Advanced Customizations of Matlab Uicontrols"
Interested readers can find more information about these and other possible customizations in my report on "Advanced Customizations of Matlab Uicontrols". This 90-page PDF report can be purchased here ($39, please allow 24 hours for delivery by email). The report explains how to customize Matlab's uicontrols in ways that are simply not possible using documented Matlab properties. This includes treatment of push buttons, toggle buttons, radio buttons, checkboxes, editboxes, listboxes, popup menus (aka combo-boxes/drop-downs), sliders, labels, and tooltips. Much of the information in the report is also available in hard-copy format in chapter 6 of my Matlab-Java programming book.
Related posts:
- Rich Matlab editbox contents The Matlab editbox uicontrol does not handle HTML contents as do other uicontrols. In this article I show how this limitation can be removed....
- Customizing listbox & editbox scrollbars Matlab listbox and multi-line editbox uicontrols have pre-configured scrollbars. This article shows how they can be customized....
- ishghandle’s undocumented input parameter The built-in function ishghandle accepts a second input argument with the expected handle type....
- Accessing plot brushed data Plot data brushing can be accessed programmatically using very simple pure-Matlab code...