how to extract certain lines from a csv file

14 visualizzazioni (ultimi 30 giorni)
Hi everyone, I really need a hand. I have a csv file containing many lines which cannot be opened in excel. I would like to create a script that allows me to keep the first line and then go and read the file line by line.
Now each line is made up of several characters, at a certain point in the line there is the element that identifies me what that line refers to (eg: UPRE_S14SPLO_901), in total there are 21 of these elements. I would like the mi script to read each line and create 21 csv files corresponding to the elements that interest me where all the lines referring to that element are contained.
Does anyone know how to do? I am attaching an example of the file to make you understand how it is structured.

Risposta accettata

Mathieu NOE
Mathieu NOE il 2 Nov 2020
hello Giuseppe !
here you are ... I converted the prova.xlsx into csv file first to be coherent with the assumption we are working with csv data
the code is below
enjoy it :
Filename = 'prova.csv';
string = 'UPRE_S14SPLO_901';
% string = 'UPRE_S14SPLO_902';
output_matrix = retrieve_csv2(Filename,string)
% % save the output to one single csv file
% output_file = 'out_prova_all.csv';
% writecell(output_matrix,output_file);
% save the output to individual csv file (title + 1 line of data)
[m,n] = size(output_matrix);
title_line = output_matrix{1,:};
for ci = 2:m
output_file = ['out_prova_ind' num2str(ci-1) '.csv'];
data_line = output_matrix{ci,:};
simple_output_matrix{1,:} = title_line; % title line (always the same)
simple_output_matrix{2,:} = data_line; % data line (increment)
writecell(simple_output_matrix,output_file);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function output_matrix = retrieve_csv2(Filename,string)
fid = fopen(Filename);
tline = fgetl(fid);
k = 1;
output_matrix{1,1} = tline;
while ischar(tline)
check = strfind(tline,string);
if ~isempty(check)
k = k+1; % loop over line index
output_matrix{k,:} = tline;
end
tline = fgetl(fid);
end
fclose(fid);
  3 Commenti
Mathieu NOE
Mathieu NOE il 3 Nov 2020
hello Giuseppe
which matlab release are you using ? I have R2020b
the function that creates the error on your side is writecell which I believe appeared in 2018
I tested some alternatives , so replace the line that creates the error
so replace :
writecell(simple_output_matrix,output_file);
with one of the following options :
xlswrite(output_file,simple_output_matrix);% Matlab R2000 and above
or as an ascii format txt (works also with csv or txt file extension). I have put the function saveascii in attachement
saveascii(simple_output_matrix,output_file) % keep csv file format output
saveascii(simple_output_matrix,'test.txt') % export as simple txt file (quite the same)
Mathieu NOE
Mathieu NOE il 3 Nov 2020
one more possible alternative is :
dlmwrite(output_file, char(simple_output_matrix),'delimiter',''); % ok

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Large Files and Big Data in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by