How to read data in single column in csv file?

1 visualización (últimos 30 días)
Abd ul Gani
Abd ul Gani el 29 de Jun. de 2021
Comentada: Abd ul Gani el 1 de Jul. de 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.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by