result in table format or excel format

4 次查看(过去 30 天)
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 个评论
Rik
Rik 2022-12-30
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
Kartick 2022-12-30
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

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2022-12-31
编辑:Stephen23 2022-12-31
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 个评论
Kartick
Kartick 2023-1-1
Sir it's working. I would like to thank you for your valuable time and support.
Thanks again for your guidance and mentorship.

请先登录,再进行评论。

更多回答(2 个)

Eric Sofen
Eric Sofen 2022-12-30
移动:Rik 2022-12-30
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 个评论
Kartick
Kartick 2022-12-31
sir
writeline function is not recognized by matlab
Jeremy Hughes
Jeremy Hughes 2022-12-31
This was introduced in R2022a, so you might be using an earlier version, (or missing the "s" at the end).

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2022-12-30
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 个评论
Kartick
Kartick 2023-1-8
移动:Rik 2023-1-8
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
Rik 2023-1-8
编辑:Rik 2023-1-8
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by