result in table format or excel format
조회 수: 2 (최근 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
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.
채택된 답변
Stephen23
2022년 12월 31일
편집: Stephen23
2022년 12월 31일
cmdout = fileread('cmdout.txt') % fake data
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))
Method two: DOUBLE:
tmp = regexprep(vertcat(tkn{:}),'^\d+$','$& NaN');
tbl = array2table(str2double(split(tmp(:,2))), 'RowNames',tmp(:,1), 'VariableNames',hdr(2:end-1))
추가 답변 (2개)
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
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!