Import Multiple Text files into Matlab
Show older comments
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 Comment
Duc Trieu (Andy)
on 8 Jun 2016
Answers (1)
Karim
on 9 Jun 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 Comment
Duc Trieu (Andy)
on 9 Jun 2016
Categories
Find more on Text Files 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!
