reading and splitting csv file

9 vues (au cours des 30 derniers jours)
Francesco Giuseppe Fornari
Hi,
I'm having troubles with a big csv file (attached).
If I try to read it as a table with readtable, MatLab displays the error: "All lines of a text file must have the same number of delimiters".
I made a check for delimiters with medhan ravi code from https://it.mathworks.com/matlabcentral/answers/418855-how-do-i-ensure-that-the-number-of-delimiters-on-each-row-are-constant-when-reading-a-hige-csv-file and apparently all the records have the same number of delimiters...
I would like to split the file in 4 tables, that I can use for elaborating datas. as you can see, each table in the source file starts with character 'I', while the other rows starts with 'D'.
I have tried with this (implementing medhan ravi code):
fid=fopen('PUBLIC_PRICES_201911180000_20191119040539.CSV ','r'); % open the file
l=fgetl(fid); % read first line
i=1; % record counter
while ~feof(fid) % rest of file
l=fgetl(fid);
if ~startsWith(l,'I')
table{i}={table{i};l};
else
i=i+1;
table{i}={l}
end
end
I tried to concatenate rows with curly brackets but didn't work.
I would really appreciate any help,
thanks you all

Réponse acceptée

Francesco Giuseppe Fornari
I solved this way:
fileID=fopen('path','r');
C=textscan(fileID,'%q','Delimiter','\n');
Index=find(contains(C{:},'I,'));
totalrows=size(C{1});
for i=1:(size(Index)-1)
D{i}=C{1}(Index(i):(Index(i+1)-1));
end
D{i+1}=C{1}(Index(i+1):totalrows);
text=char(D{1});
T=table;
titoli=strsplit(text(1,:),',');
titoli([20:21 23:29 31:37 39:45 47:53 55:61 63:67 73 75:77 79:96 ])=[];
for i=1:((size(text,1))-1)
T(i,:)=strsplit(text(i+1,:),',');
end
T.Properties.VariableNames=titoli;

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by