How to read data in single column in csv file?

7 ビュー (過去 30 日間)
Abd ul Gani
Abd ul Gani 2021 年 6 月 29 日
コメント済み: Abd ul Gani 2021 年 7 月 1 日
I have attached the csv file I am working with. I have imported the file through ' readtable'. Now I have to read the data in the columns.
For E.g, 1. From the column- file_attributes, {"frame_id":"1"}, I need to make a sepaerate column with 1,2,3,& 4 with header" Frame ID",
2. From the column region_shape_attributes , {"name":"rect","x":101,"y":30,"width":239,"height":244}, I have to build a vector [ 101 30 239 244].
How can this be done?
N.B: Splitting single cell to multiple cells make it cumbersome.


Johannes Hougaard
Johannes Hougaard 2021 年 6 月 29 日
Seems like a pretty hard way to do stuff - that the .csv file is heavily formatted.
Having formatted strings - to me - calls for the use of regular expressions, but thats kind of a jungle.
But it's probably doable in a reasonable generic and fast way
This is a one-line code for the first might prefer to substitute the cellfun for a foor loop and to do one operation at a time.
myfile = readtable("myfile.csv");
myfile = addvars(myfile,cellfun(@str2double,regexpi(myfile.file_attributes,'\"(\d+)\"','tokens','once')),'NewVariableNames','Frame ID');
and the vector I couldn't fix in a oneliner without a for-loop...but I guess this'll do it
temporary_variable = regexpi(myfile.region_shape_attributes,'(\d+)','tokens');
vector = nan(size(temporary_variable,1),4);
for ii = 1:size(temporary_variable,1)
vector(ii,:) = cellfun(@str2double,temporary_variable{ii});
clear ii temporary_variable
  1 件のコメント
Abd ul Gani
Abd ul Gani 2021 年 7 月 1 日
myfile = addvars(myfile,cellfun(@str2double,regexpi(myfile.file_attributes,'\"(\d+)\"','tokens','once'),'UniformOutput',false),'NewVariableNames','Frame ID');


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by