6 views (last 30 days)
Show older comments
Kartick on 30 Dec 2022
-
-
Link
Direct link to this question
https://support.mathworks.com/matlabcentral/answers/1886412-result-in-table-format-or-excel-format
Edited: Rik on 8 Jan 2023
Accepted Answer: Stephen23
Open in MATLAB Online
I am using following code:
command='netstat -e'
[status,cmdout]=system(command)
Result:
cmdout =
'Interface Statistics
Received Sent
Bytes 4178033452 510183200
Unicast packets 124836 2463020
Non-unicast packets 30732 38568
Discards 0 0
Errors 0 0
Unknown protocols 0
'
The cmdout is char file (1x371).
I wish to get this data in table form (row x column) or in excel format or as matrix. Please guide. Thank you in advance
2 Comments Show NoneHide None
Show NoneHide None
Rik on 30 Dec 2022
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1886412-result-in-table-format-or-excel-format#comment_2540597
What have you tried so far yourself? Splitting the char on newlines is the easiest part, but splitting the char to columns should not be too hard either.
Kartick on 30 Dec 2022
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1886412-result-in-table-format-or-excel-format#comment_2540682
I have tried following things:
str=convertCharsToStrings(cmdout)
t=table(str)
cel=table2cell(t)
array=table2array(t)
st=table2struct(t)
s=reshape(array,[1,15])
but none has helped. Thanks for response
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Stephen23 on 31 Dec 2022
Edited: Stephen23 on 31 Dec 2022
Open in MATLAB Online
- cmdout.txt
cmdout = fileread('cmdout.txt') % fake data
cmdout =
'Interface Statistics Received Sent Bytes 4178033452 510183200 Unicast packets 124836 2463020 Non-unicast packets 30732 38568 Discards 0 0 Errors 0 0 Unknown protocols 0 '
hdr = regexp(cmdout,'^\s*(\S+\s\S+)\s+(\S+)\s+(\S+)\s+(.*)','tokens','once');
tkn = regexp(hdr{end},'(\S+(\s\S+)*)\s+(\d+(\s+\d+)*)','tokens');
Method one: UINT64:
tmp = vertcat(tkn{:});
mat = sscanf(sprintf(' %s',tmp{:,2}),'%lu',[2,Inf]).';
tbl = array2table(mat, 'RowNames',tmp(:,1), 'VariableNames',hdr(2:end-1))
tbl = 6×2 table
Received Sent __________ _________ Bytes 4178033452 510183200 Unicast packets 124836 2463020 Non-unicast packets 30732 38568 Discards 0 0 Errors 0 0 Unknown protocols 0 0
Method two: DOUBLE:
tmp = regexprep(vertcat(tkn{:}),'^\d+$','$& NaN');
tbl = array2table(str2double(split(tmp(:,2))), 'RowNames',tmp(:,1), 'VariableNames',hdr(2:end-1))
tbl = 6×2 table
Received Sent __________ __________ Bytes 4.178e+09 5.1018e+08 Unicast packets 1.2484e+05 2.463e+06 Non-unicast packets 30732 38568 Discards 0 0 Errors 0 0 Unknown protocols 0 NaN
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Kartick on 1 Jan 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1886412-result-in-table-format-or-excel-format#comment_2542227
Sir it's working. I would like to thank you for your valuable time and support.
Thanks again for your guidance and mentorship.
Sign in to comment.
More Answers (2)
Eric Sofen on 30 Dec 2022
Moved: Rik on 30 Dec 2022
It's a little circuitous, but you can use writelines to write the char vector to a text file, then use the power of readtable to parse it into a table in MATLAB.
3 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Jeremy Hughes on 30 Dec 2022
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1886412-result-in-table-format-or-excel-format#comment_2541072
Edited: Jeremy Hughes on 30 Dec 2022
Open in MATLAB Online
% command='netstat -e';
% [status,cmdout]=system(command)
Above doesn't work on Answers, so simulating the output:
cmdout = [
"Interface Statistics";
"";
" Received Sent";
""
"Bytes 4178033452 510183200";
"Unicast packets 124836 2463020";
"Non-unicast packets 30732 38568";
"Discards 0 0";
"Errors 0 0";
"Unknown protocols 0"];
writelines(cmdout,"data.txt");
T = readtable("data.txt","FileType","fixedwidth","NumHeaderLines",2)
Kartick on 31 Dec 2022
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1886412-result-in-table-format-or-excel-format#comment_2541467
⋮
sir
writeline function is not recognized by matlab
Jeremy Hughes on 31 Dec 2022
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1886412-result-in-table-format-or-excel-format#comment_2541912
https://www.mathworks.com/help/matlab/ref/writelines.html
This was introduced in R2022a, so you might be using an earlier version, (or missing the "s" at the end).
Sign in to comment.
Walter Roberson on 30 Dec 2022
Open in MATLAB Online
You have fixed-width columns. Use array indexing:
topic = string(cmdout(5:end, 1:22));
received = double(string(cmdout(5:end, 23:35)));
sent = double(string(cmdout(5:end, 36:end)));
T = table(topic, received, sent);
7 Comments Show 5 older commentsHide 5 older comments
Show 5 older commentsHide 5 older comments
Kartick on 31 Dec 2022
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1886412-result-in-table-format-or-excel-format#comment_2541462
Sir, The return result is :
topic =
0×1 empty string array
received =
0×1 empty double column vector
sent =
0×1 empty double column vector
T =
0×3 empty table
sir, there is no data.
Rik on 31 Dec 2022
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1886412-result-in-table-format-or-excel-format#comment_2541517
This indexing assumes a char array split into multiple lines, so that step should still be implemented. I don't understand why you did not receive an error instead of empty arrays.
Walter Roberson on 1 Jan 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1886412-result-in-table-format-or-excel-format#comment_2542867
Open in MATLAB Online
cmdout = char(regexp(cmdout, '\r?\n', 'split'));
topic = string(cmdout(5:end, 1:22));
received = double(string(cmdout(5:end, 23:35)));
sent = double(string(cmdout(5:end, 36:end)));
T = table(topic, received, sent);
Kartick on 8 Jan 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1886412-result-in-table-format-or-excel-format#comment_2551967
Thank you very much sir. This is working.
Rik on 8 Jan 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1886412-result-in-table-format-or-excel-format#comment_2552072
If a solution works, you should mark it as accepted answer.
Kartick on 8 Jan 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1886412-result-in-table-format-or-excel-format#comment_2552287
Moved: Rik on 8 Jan 2023
Sir actually I have marked a response as accepted which was suggested earlier. This too was also working and I am unable to accept this answer.
Rik on 8 Jan 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/1886412-result-in-table-format-or-excel-format#comment_2552292
Edited: Rik on 8 Jan 2023
No, you flagged a comment. A flag is meant to attract the attention of an admin, not to indicate the solution to a question.
You can indeed not accept a second answer. What you can do is unaccept the previous one, and accept this answer.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsData TypesData Type Conversion
Find more on Data Type Conversion 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!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office