How to read data in single column in csv file?

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

Johannes Hougaard
Johannes Hougaard il 29 Giu 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
  1 Commento
Abd ul Gani
Abd ul Gani il 1 Lug 2021
***
myfile = addvars(myfile,cellfun(@str2double,regexpi(myfile.file_attributes,'\"(\d+)\"','tokens','once'),'UniformOutput',false),'NewVariableNames','Frame ID');

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by