result in table format or excel format
古いコメントを表示
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 件)
Eric Sofen
2022 年 12 月 30 日
移動済み: Rik
2022 年 12 月 30 日
1 投票
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 件のコメント
Jeremy Hughes
2022 年 12 月 30 日
編集済み: Jeremy Hughes
2022 年 12 月 30 日
% 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
2022 年 12 月 31 日
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
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
2022 年 12 月 31 日
Rik
2022 年 12 月 31 日
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
2023 年 1 月 1 日
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
2023 年 1 月 8 日
Rik
2023 年 1 月 8 日
If a solution works, you should mark it as accepted answer.
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!