Get IP address over a Edit box

24 views (last 30 days)
Vick on 4 Jan 2018
Edited: Image Analyst on 19 Sep 2022
Hi all,
I need to get a IP address kind of values from the edit box.i.e, I have to get like 10 values from one edit box. I've planned to give an edit box like image shown below,
How to do this in edit box . I remember seeing this in some undocumented matlab using combo boxes but i can't recall it.

Accepted Answer

Yair Altman
Yair Altman on 5 Jan 2018
Edited: Yair Altman on 5 Jan 2018
Matlab comes with a built-in IP component, com.jidesoft.field.IPTextField, which is part of the JIDE library of components. You can use it as follows:
jIPField = com.jidesoft.field.IPTextField(''); % set default IP
[jIPField, hContainer] = javacomponent(jIPField, [10,10,120,20], hParent); % hParent: panel/figure handle
You can modify the position/size of the text-field in the javacomponent call above, or by modifying the Position / Units properties of hContainer.
You 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 => ''
The component auto-validates the IP values, ensuring that the displayed IP is always valid. The component has many other features, including the ability to enable/disable, color or format the IP components etc.
You 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 selectors, credit-card fields, etc. etc.
For more information about using the javacomponent function and handling Java components in Matlab GUI, see my website or book (" Undocumented Secrets of Matlab-Java Programming ", CRC Press 2014)
Yair Altman
Yair Altman
Yair Altman on 5 Jan 2018
Hi Jan - I do indeed have keyword scans in place, and that's how I got here. In the good ol' days of CSSM (RIP), traffic was lower, I had more spare time, and I also got a daily digest of all new threads, so I could quickly review everything. But nowadays I no longer have time to review all the new posts, and I no longer get a digest, so I just use keyword filters (which obviously ignores many potentially interesting posts).
Bottom line: if you want to get my attention, then writing the words "Yair" or "undocumented" will have a good chance of doing this...

Sign in to comment.

More Answers (2)

Jan on 4 Jan 2018
Edited: Jan on 4 Jan 2018
You can use a standard edit box and perform the checks in the callback, e.g.:
function EditCallback(hObject, EventData, handles)
Str = get(hObject, 'String');
[Num, N] = sscanf(Str, '%d.%d.%d.%d');
if N == 4 && && all(Num > 0) && all(Num < 256)
Str = sprintf('%03d.%03d.%03d.%03d', Num); % Or: '%d.%d.%d.%d' if you like it
set(hObject, 'String', Str, 'Color', [0,0,0]);
handles.IP = Str;
guidata(hObject, handles);
else % Warn the user, that this is no valid IP:
set(hObject, 'Color', [1,0,0]);

Image Analyst
Image Analyst on 19 Sep 2022
Edited: Image Analyst on 19 Sep 2022
A related answer, if you want to get the IP address of your own computer, you can do this (if you're using a Windows system):
[status, commandOutput] = system('ipconfig')
str = 'IPv4 Address. . . . . . . . . . . : ';
% Extract ip addresses. There will be one for each network adapter, e.g. wired, wireless, etc.
index = strfind(commandOutput, str);
startingIndex = index + length(str)
for k = 1 : length(startingIndex)
s = commandOutput(startingIndex(k):startingIndex(k)+15);
% Get rid of any trailing non-numeric characters
while isnan(str2double(s(end))) && length(s) > 1
s = s(1 : end-1);
ipAddress{k} = s;
fprintf('IP Address #%d = %s\n', k, s);


Find more on Environment and Settings in Help Center and File Exchange


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by