How to read data in single column in csv file?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Abd ul Gani
am 29 Jun. 2021
Kommentiert: Abd ul Gani
am 1 Jul. 2021
Hi,
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.
0 Kommentare
Akzeptierte Antwort
Johannes Hougaard
am 29 Jun. 2021
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 operation...you 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});
end
clear ii temporary_variable
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Cell Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!