How to read data in single column in csv file?

조회 수: 2 (최근 30일)
Abd ul Gani
Abd ul Gani 2021년 6월 29일
댓글: Abd ul Gani 2021년 7월 1일
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.

채택된 답변

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 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
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개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by