Import Multiple Text files into Matlab

7 visualizaciones (últimos 30 días)
Duc Trieu (Andy)
Duc Trieu (Andy) el 8 de Jun. de 2016
Comentada: Duc Trieu (Andy) el 9 de Jun. de 2016
Hi Everyone, I'm beginner with Matlab. I need to import multiple .txt files into Matlab with the format:
Symbol, Date, Time, Price, Volume
CLF2000, 02/23/2014, 9:48:00, 123, 0
CLF2000, 02/23/2014, 9:23:00, 121, 0
Then, I want to delete the column Volume, delete row have price 0 and NaN. Next, I need to combine the column 2 and 3 Firstly, I made a function as follows:
function data=getdatafromfiles_singlefile(filenames)
data(1).Symbol=[];
data(1).Date=[];
data(1).Time=[];
data(1).Price=[];
data(1).Volume=[];
num=0;
for i=1:numel(filenames)
fID=fopen(filenames{i});
Ci=textscan(fID,'%s %{MM/dd/yyyy}D %{hh:mm:ss} %f %f''Headerlines',1,'delimiter',',');
fclose(fID);
for j=1:numel(Ci{1})
data(num+j).Symbol=Ci{1,1}(j);
data(num+j).Date=Ci{1,2}(j);
data(num+j).Time=Ci{1,3}(j);
data(num+j).Price=Ci{1,4}(j);
data(num+j).Volume=Ci{1,5}(j);
end
num=num+numel(Ci{1});
end
Then, I wrote the below code using the above function:
% %%reset
clear all;
close all;
clc;
for k = 1996:1:1997;
CLF = getdatafromfiles_singlefile({['CLF',num2str(k),'.txt']});
CLF(isnan([CLF.Price])==1)=[];
CLF([CLF.Price]==0)=[];
% Remove the field called Volume
hasField = isfield(CLF, 'Volume');
if hasField
CLH = rmfield(CLF, 'Volume');
else
end
eval(['CLF' num2str(k) '=CLF']);
end
However, my code faced the below errors:
Error using textscan
Unable to parse the format string at position 19 ==> %{hh:mm:ss} %f %f'Headerlines
Date formats must be of the form %T or %{...}T.
Error in getdatafromfiles_singlefile (line 10)
Ci=textscan(fID,'%s %{MM/dd/yyyy}D %{hh:mm:ss} %f %f''Headerlines',1,'delimiter',',');
Error in getdatafromfiles_importsinglefile (line 7)
CLF = getdatafromfiles_singlefile({['CLF',num2str(k),'.txt']});
Could you please give me advice about my error?
Thanks in advance.
  1 comentario
Duc Trieu (Andy)
Duc Trieu (Andy) el 8 de Jun. de 2016
Hi
I already tried many ways. But It doesn't work.
When I use : " Ci=textscan(fID,'%s %{MM/dd/yyyy}D %{hh:mm:ss}D %f %f''Headerlines',1,'delimiter',',');
I got the result as picture
Or when I used: Ci=textscan(fID,'%s %s %s %f %f''Headerlines',1,'delimiter',',');
The result has '' in column. I don't know it will work or not when I call this number to use.
Could anyone please help me? Thanks

Iniciar sesión para comentar.

Respuestas (1)

Karim
Karim el 9 de Jun. de 2016
you are missing a comma after your file specifier:
change
Ci=textscan(fID,'%s %{MM/dd/yyyy}D %{hh:mm:ss}D %f %f''Headerlines',1,'delimiter',',');
into
Ci=textscan(fID , '%s%{MM/dd/yyyy}D%{hh:mm:ss}D%f%f' , 'Headerlines' , 1 , 'delimiter' , ',');
Regards
  1 comentario
Duc Trieu (Andy)
Duc Trieu (Andy) el 9 de Jun. de 2016
Hi KASR,
It is still the same with my picture when I add the comma into my code.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by