- Undocumented Matlab - https://undocumentedmatlab.com -

IP address input control

Posted By Yair Altman On January 31, 2018 | 3 Comments

A few weeks ago, a user posted a question on Matlab Answers [1], asking whether it is possible to implement a text input control that accepts and validates an IP address (for example, ‘192.168.1.101’). While doing this using purely documented Matlab code is indeed possible (for those of us who are masochistically inclined and/or have nothing else to do with their spare time), a very simple-to-use and polished-looking solution is to use an undocumented built-in Matlab control.
The solution is based on the fact that Matlab comes with a huge set of professional Java-based controls by JideSoft, bundled in various JAR libraries within the %matlabroot%/java/jarext/jide Matlab installation folder. For our specific purposes (an IP-address entry/display control), we are interested in the com.jidesoft.field.IPTextField control (online documentation [2]), which is part of the JIDE Grids library (%matlabroot%/java/jarext/jide/jide-grids.jar). We can use it as follows:

jIPField = com.jidesoft.field.IPTextField('255.255.255.0');  % set default IP
[jIPField, hContainer] = javacomponent(jIPField, [10,10,120,20], hParent);  % hParent: panel/figure handle

IPTextField control in a Matlab GUI
IPTextField control in a Matlab GUI

You can modify the position/size of the text-field in the javacomponent call above, or by modifying the Position / Units properties of the returned hContainer.
We can retrieve the IP text/numeric values using:

vals = jIPField.getValue';         % 1x4 uint32 array => [255,255,255,0]
vals = cell(jIPField.getRawText)'; % 1x4 string cells => {'255','255','255','0'}
ip   = char(jIPField.getText);     % entire IP string => '255.255.255.0'

The IPTextField component auto-validates the IP values, ensuring that the displayed IP is always valid (for example, IP components cannot be negative or larger than 255). The component has many other features, including the ability to enable/disable, color or format the IP components etc.

We can set a callback function to process user changes, by setting the component’s *StateChangedCallback* property, for example:

jIPField.StateChangedCallback = @(jComponent,jEventData) disp(jComponent.getValue');

The JIDE libraries that come with Matlab contain numerous other similarly-useful components, including date/time/font/color/file/folder selectors [3], calendars in various formats [4], credit-card fields, and many more.
For more information about using the javacomponent function [5] and handling Java components in Matlab GUI, see other posts on this website – particularly those marked with the “JIDE” tag [6].
Additional discussion of JIDE’s combo-boxes, and JIDE controls in general, is available in Chapter 5 of my Matlab-Java Programming book [7].
If you need to integrate professional-looking controls such as these in your Matlab GUI, consider hiring my consulting services [8].

Caution

Remember that JIDE evolves with Matlab, and so JIDE’s online documentation, which refers to the latest JIDE version, may be partially inapplicable if you use an old Matlab version. In any case, Matlab releases always lag the latest JIDE release by at least a year (e.g., Matlab R2017b still uses JIDE v3.4.1 that was released in June 2012 – MathWorks used to update the bundled JIDE libraries to newer versions, but for some reason has stopped doing that in 2013). The older your Matlab, the more such inconsistencies that you may find. For example, I believe that DateSpinnerComboBox only became available around R2010b; similarly, some control properties behave differently (or are missing altogether) in different releases. To determine the version of JIDE that you are currently using in Matlab, run the following (the result can then be compared to JIDE’s official change-log history [9]):

>> com.jidesoft.utils.Lm.getProductVersion
ans =
3.4.1

Note that JIDE is a commercial product. We may not use it without JIDESoft’s permission outside the Matlab environment. It is my understanding however, that we can freely use it within Matlab. Note that this is not legal advise as I am an engineer, not a lawyer. If you have any licensing questions, contact sales@jidesoft.com.
Also note that all of JIDE’s controls use the Java-based figures (that are created using GUIDE or the figure function), and will not work on the new web-based uifigures (created using App Designer or the uifigure function). There is currently no corresponding IP entry/display control for web-based GUIs, and since there is [still] no way to integrate external Javascript/CSS libraries in uifigures, the only resort is to use a plain-vanilla edit-box. If MathWorks would simply open a hook to integrate external JS/CSS libraries, that would enable users to use 3rd-party libraries that have such custom controls and MathWorks would then not need to spend a huge amount of effort to develop multiple UI control variants.

Categories: GUI, High risk of breaking in future versions, Java, UI controls, Undocumented feature


3 Comments (Open | Close)

3 Comments To "IP address input control"

#1 Comment By Hanan Kavitz On February 4, 2018 @ 10:26

Thanks Yair,
Changed one of my edit boxes that are IP address input to use IPTextField
🙂

#2 Comment By Peter Cook On February 7, 2018 @ 20:32

A quick method to check if a machine is connected to a particular IP address using MATLAB, which is a potentially useful callback for a widget like this (after string verified with IPTextField):

%check to see if user is on corporate ethernet and terminate otherwise
[~,dosReply] = dos('ping 12.34.56.78');
[~,~,noPacket] = regexp(dosReply,'Received = \d','match');
noPacket = str2double(dosReply(noPacket));
if lt(noPacket,2)
    uiwait(errordlg('Must be Connected to Secure Network'))
    exit
end

#3 Comment By Lokesh Sharma On March 30, 2018 @ 13:16

this is a good method…


Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/articles/ip-address-input-control

URLs in this post:

[1] a question on Matlab Answers: https://www.mathworks.com/matlabcentral/answers/375588-get-ip-address-over-a-edit-box

[2] online documentation: http://www.jidesoft.com/javadoc/com/jidesoft/field/IPTextField.html

[3] date/time/font/color/file/folder selectors: http://undocumentedmatlab.com/blog/using-jide-combo-boxes

[4] calendars in various formats: http://undocumentedmatlab.com/blog/date-selection-components

[5] javacomponent function: http://undocumentedmatlab.com/blog/javacomponent

[6] “JIDE” tag: http://undocumentedmatlab.com/blog/tag/jide

[7] Matlab-Java Programming book: http://undocumentedmatlab.com/books/matlab-java

[8] consulting services: http://undocumentedmatlab.com/consulting/

[9] change-log history: http://www.jidesoft.com/history/

[10] Editbox data input validation : https://undocumentedmatlab.com/articles/editbox-data-input-validation

[11] ishghandle's undocumented input parameter : https://undocumentedmatlab.com/articles/ishghandle-undocumented-input-parameter

[12] Date selection components : https://undocumentedmatlab.com/articles/date-selection-components

[13] JIDE Property Grids : https://undocumentedmatlab.com/articles/jide-property-grids

[14] FindJObj GUI – display container hierarchy : https://undocumentedmatlab.com/articles/findjobj-gui-display-container-hierarchy

[15] Uitable sorting : https://undocumentedmatlab.com/articles/uitable-sorting

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.