Get IP address over a Edit box

16 次查看(过去 30 天)
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.

采纳的回答

Yair Altman
Yair Altman 2018-1-5
编辑:Yair Altman 2018-1-5
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('255.255.255.0'); % 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 => '255.255.255.0'
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
  2 个评论
Jan
Jan 2018-1-5
+1. Yair, do you read all questions in the forum and post such perfect answers if they belong to the Java treasure? Or do you let a robot scan the forum for the term "undocumented"? I thought about this in the times of CSSM already. Sometimes I simply added "This is a question for Yair" in my answers to catch your attraction, and you posted an answer an hour later. Very attentive. Thanks.
Yair Altman
Yair Altman 2018-1-5
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...

请先登录,再进行评论。

更多回答(2 个)

Jan
Jan 2018-1-4
编辑:Jan 2018-1-4
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]);
end
end

Image Analyst
Image Analyst 2022-9-19
编辑:Image Analyst 2022-9-19
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);
end
ipAddress{k} = s;
fprintf('IP Address #%d = %s\n', k, s);
end

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by