Convert 1x1 cell array to double

8 ビュー (過去 30 日間)
Pablo
Pablo 2023 年 8 月 2 日
コメント済み: Pablo 2023 年 8 月 2 日
Hi all,
I am trying to extract some data for plotting from a .csv file (find attached obtained from a simulation program. The problem is, when I import the data as a table, then the values I want to extract have the following form:
1x1 cell array : {'0.000202796 0.000207569 0.000211678 0.001893233 0.001898714 0.001899608 0.008668105 ... '}
I manage to extract the data succesfully (rttTime double vector) , but I am not sure if there is a more efficient way of doing it:
It would be great to have suggestions, how to improve it, also I am open for suggestions concerning the import of data from the .csv file.
Kind regards,
Pablo
% read data with all stats vector
statsTable = readtable("stats_1-trace_vector_example.csv", 'PreserveVariableNames', true);
% Extract index of row containing RTT
rttIdx = statsTable.name == "rtt:vector" & statsTable.attrname == "";
rttTable = statsTable(rttIdx,:);
rttTime = str2double(strsplit(cell2mat(rttTable.vectime)));
rttValues = str2double(strsplit(cell2mat(rttTable.vecvalue)));
plot(rttTime,rttValues);
  2 件のコメント
Stephen23
Stephen23 2023 年 8 月 2 日
編集済み: Stephen23 2023 年 8 月 2 日
Given that rather unfortunate file format (storing vectors of numeric data in quoted strings), your approach seems reasonable and understandable. Of course if efficiency is really the goal then you should replace CELL2MAT with some basic indexing into that cell array. SSCANF might be the fastest approach.
Pablo
Pablo 2023 年 8 月 2 日
All right thank you very much Stephen23! Yes efficiency and specially code cleanness is the goal, that's why I find your approach quite nice, since I don't need the intermediate table.
Yes I know the file format is not the best, it was generated from results of OMNET++ using scavetool. I might have a look how to improve it...

サインインしてコメントする。

回答 (2 件)

Stephen23
Stephen23 2023 年 8 月 2 日
編集済み: Stephen23 2023 年 8 月 2 日
Here with no intermediate table:
statsTable = readtable("stats_1-trace_vector_example.csv", 'PreserveVariableNames', true);
rttIdx = statsTable.name == "rtt:vector" & statsTable.attrname == "";
rttTime = sscanf(statsTable{rttIdx,'vectime'}{1},'%f',[1,Inf]);
rttValues = sscanf(statsTable{rttIdx,'vecvalue'}{1},'%f',[1,Inf]);
plot(rttTime,rttValues);
rttTable = statsTable(rttIdx,:);
timeit(@() str2double(strsplit(cell2mat(rttTable.vectime))))
ans = 7.0269e-04
timeit(@() str2double(strsplit(rttTable.vectime{1})))
ans = 6.3669e-04
timeit(@() str2num(rttTable.vectime{1}))
ans = 4.0264e-05
timeit(@() sscanf(rttTable.vectime{1},'%f',[1,Inf]))
ans = 3.7764e-05

Bruno Luong
Bruno Luong 2023 年 8 月 2 日
Not sure if it's more efficient but it's shorter
rttTime = str2num(rttTable.vectime{1})
  2 件のコメント
Bruno Luong
Bruno Luong 2023 年 8 月 2 日
Yes it's more efficient as well
statsTable = webread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1448087/stats_1-trace_vector_example.csv');
rttIdx = statsTable.name == "rtt:vector" & statsTable.attrname == "";
rttTable = statsTable(rttIdx,:);
rttTime = str2double(strsplit(cell2mat(rttTable.vectime)));
timeit(@() str2double(strsplit(cell2mat(rttTable.vectime))))
ans = 6.3177e-04
timeit(@() str2num(rttTable.vectime{1}))
ans = 4.2768e-05
Pablo
Pablo 2023 年 8 月 2 日
Thank you very much Bruno for your answer!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by