Read data from complex *csv file
Show older comments
Hello everyone! I've been trying to read data from a .csv file that looks like:
[[0.714792626301598, -0.697224229221414, 0.05431275698645074], 0, 0, 'pos_0'],[[-0.5884201907614969, 0.7739447885416152, -0.23403235544146034], 1, 0, 'pos_1'],[[0.1944985746440766, -0.9746176263199874, 0.11086382154614778], 50, (0, 2), 'pos_4']....
The idea is to get separately for each column:
[0.714792626301598, -0.697224229221414, 0.05431275698645074]
0
0 %this could be in a (0, 2) format as displayed in the third column
'pos_0'
The file is around 200 columns, I've tried the following code:
S = textscan(fid,'%*c %[^] %*c %f %*c %f %s]','Delimiter',',');
resulting in a badly ordered cell array. Any help will be much appreciated!!
Thanks!
3 Comments
@Pablo: do not give us screenshots of data: we cannot import screenshots of data, we cannot test code on screenshots of data, we cannot search screenshots of data, we cannot edit screenshots of data, we cannot use screenshots of data in any meaningful way.
Please upload the actual text data file (or a file that represents all of its salient features).
Pablo
on 2 Nov 2018
Ugh, what a badly formatted "CSV" file: it is a liberal mess of double quotes, square brackets, and superfluous single quotes... there are random parentheses around two fields, double quotes surround the entirety of each "line", and there is only one newline at the very end of the file! It should be named "Incoherence_disorder.txt".
The best solution to your problem is fix the thing that created that awful file.
Accepted Answer
More Answers (1)
madhan ravi
on 2 Nov 2018
Edited: madhan ravi
on 2 Nov 2018
fid=fopen('coherence_order.csv','r')
f = textscan(fid,'%s','delimiter',{']'})
fclose(fid)
7 Comments
KSSV
on 2 Nov 2018
f = textscan(fid,'%s','delimiter','\n')
This takes data as it is in the file. But the user wants all three columns separated.
madhan ravi
on 2 Nov 2018
Edited: madhan ravi
on 2 Nov 2018
This takes data as it is in the file
No it doesn’t, instead it reads all of them as separate cells
But the user wants all three columns separated.
True
Pablo
on 2 Nov 2018
KSSV
on 2 Nov 2018
{'[[0.714792626301598, -0.697224229221414, 0.05431275698645074], 0, 0, 'pos_0'],' }
{'[[-0.5884201907614969, 0.7739447885416152, -0.23403235544146034], 1, 0, 'pos_1'],' }
{'[[0.1944985746440766, -0.9746176263199874, 0.11086382154614778], 50, (0, 2), 'pos_4'],'}
The above is the result of:
f = textscan(fid,'%s','delimiter','\n')
It text scans the file as it is present in the file.
madhan ravi
on 2 Nov 2018
did you try this?
f = textscan(fid,'%s','delimiter',{']'})
KSSV
on 2 Nov 2018
{'[[0.714792626301598, -0.697224229221414, 0.05431275698645074' }
{', 0, 0, 'pos_0'' }
{',' }
{'[[-0.5884201907614969, 0.7739447885416152, -0.23403235544146034'}
{', 1, 0, 'pos_1'' }
{',' }
{'[[0.1944985746440766, -0.9746176263199874, 0.11086382154614778' }
{', 50, (0, 2), 'pos_4'' }
{','
madhan ravi
on 2 Nov 2018
Edited: madhan ravi
on 2 Nov 2018
It is not the expected solution however because of the file arrangement
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!