Extract the Transport names from the list of adapter names

4 views (last 30 days)
Hello,
I am trying to create a list from the following 1*554 char array that gets created in Maltab after doing "[g, f] = system('getmac)'"
'
Physical Address Transport Name
=================== ==========================================================
08-6A-C5-47-AD-F1 \Device\Tcpip_{D8D48A09-8BEC-4AEB-A8B4-3FB066879E29}
08-6A-C5-47-AD-F5 Media disconnected
E0-70-EA-F2-80-43 Media disconnected
02-50-41-00-00-01 \Device\Tcpip_{179E0B44-9590-47E9-9BD6-22A6F2889C37}
0A-00-27-00-00-2D \Device\Tcpip_{C13C63B9-ACD8-4FFD-B754-E0C5D3386967}
'
Now my question is how do I build a list of only the adapter names(Transport names) that starts with \Device and ends with }
so basically want to create a list/variable in matlab as follows that I can then list as user options into my GUI as a drop down menu.
list = \Device\Tcpip_{D8D48A09-8BEC-4AEB-A8B4-3FB066879E29}
\Device\Tcpip_{179E0B44-9590-47E9-9BD6-22A6F2889C37}
\Device\Tcpip_{C13C63B9-ACD8-4FFD-B754-E0C5D3386967}
I am having troubles to come up with a regexp pattern for this. Can anyone provide some hints ?
  1 Comment
chrisw23
chrisw23 on 27 Jul 2022
This can be done with String operations. Look for extract extractBefore extractAfter and Pattern options.

Sign in to comment.

Accepted Answer

dpb
dpb on 27 Jul 2022
I was going after @chrisw23's mode as I have a terrible time with regexp -- I can find both the start and the end of the string, but I cannot get it to return the token if I put both together in one expression similar to the example in the doc. I've no klew what's wrong with it nor what to do to try to fix it so I do as per usual -- get frustrated-enough to go at it somehow else.
This one isn't too bad -- I'm sure a whizard could do it in a one-liner with regexp, but
f=strtrim(split(f,newline))
f=strtrim(split(f(contains(f,'\Device'))))
list=f(:,2)
seems to do the trick ok. Or, you could use the new-fangled strings if cared to.
I'm frustrated with extractXXX and friends -- there isn't an extractFrom to pull at the matching string; only before or after the match--here and so often it leaves you without the beginning portion one would like. On occasion I've resorted to inserting a code character into the string first, but that's a hack. Otherwise, one has to use another strfind operation to locate the character position and extract by position.
regexp would undoubtedly still be a more elegant solution, but it'll take a better guru than I...or at least am willing to spend any further time over; any lesson I learned I'll have forgotten by the next time wanted it.
  2 Comments
chrisw23
chrisw23 on 16 Aug 2022
import System.Net.NetworkInformation.*
nics = NetworkInterface.GetAllNetworkInterfaces();
enNics = nics.GetEnumerator();
while enNics.MoveNext
curNic = enNics.Current;
details(curNic) % select what you need (see available properties and methods)
% example data struct
iName = matlab.lang.makeValidName(string(curNic.Name));
Intf.(iName).Description = string(curNic.Description.ToString());
Intf.(iName).OpState = curNic.OperationalStatus;
Intf.(iName).PhysicalAddress = string(curNic.GetPhysicalAddress.ToString());
Intf.(iName).IsAvailable = string(curNic.GetIsNetworkAvailable);
end
beside the RegExp/String topic......modify this to get all information you need about NetworkInterfaces on your system

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 16 Aug 2022
Edited: Stephen23 on 16 Aug 2022
Oneliner :)
txt = 'Physical Address Transport Name =================== ========================================================== 08-6A-C5-47-AD-F1 \Device\Tcpip_{D8D48A09-8BEC-4AEB-A8B4-3FB066879E29} 08-6A-C5-47-AD-F5 Media disconnected E0-70-EA-F2-80-43 Media disconnected 02-50-41-00-00-01 \Device\Tcpip_{179E0B44-9590-47E9-9BD6-22A6F2889C37} 0A-00-27-00-00-2D \Device\Tcpip_{C13C63B9-ACD8-4FFD-B754-E0C5D3386967} ';
out = regexp(txt,'\\Device[^}]+}','match');
out(:)
ans = 3×1 cell array
{'\Device\Tcpip_{D8D48A09-8BEC-4AEB-A8B4-3FB066879E29}'} {'\Device\Tcpip_{179E0B44-9590-47E9-9BD6-22A6F2889C37}'} {'\Device\Tcpip_{C13C63B9-ACD8-4FFD-B754-E0C5D3386967}'}

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by