Error during import an ordered sequence of csv file

2 views (last 30 days)
Hi, i've an ordered sequence of 17225 files and i want to import all of them. I used this code:
function [ LTE_Matrix ]= ProcessManyLTEfilesInOneFolder( FolderContainingCSVfiles, CsvFileNameForOutput )
if nargin<1
FolderContainingCSVfiles = uigetdir;
end
InterestingFiles = [ FolderContainingCSVfiles filesep '\*.CSV'];
dirListing = dir(InterestingFiles);
VectorOfFileNames = {dirListing.name};
NumberOfFiles = length(VectorOfFileNames(not([dirListing.isdir])));
if NumberOfFiles==0
error('In the folder there are no CSV files');
else
disp(['There are ' num2str(NumberOfFiles) ' CSV files that are going to be processed ']);
end
LTE_Matrix = zeros(1, NumberOfFiles);
for IndexOfFileName=1:NumberOfFiles
CurrentFullFileName = fullfile(FolderContainingCSVfiles, VectorOfFileNames{IndexOfFileName});
disp(['Processing file number ' num2str(IndexOfFileName) ' (out of ' num2str(NumberOfFiles) '). Name: ' CurrentFullFileName ]);
[LTE_Matrix(1,IndexOfFileName)]=test(CurrentFullFileName);
end %for
if nargin>1
disp('Writing output csv file');
csvwrite(CsvFileNameForOutput, LTE_Matrix);
end
end
The program imports only 16383 files and, when it arrives at file1000.csv, it imports files from file10000.csv to file10010.csv, then it returns to file1001.csv and it restarts with file10011.csv and so on.. Is there anyone that could help me? I would that it imports files in the correct order because they are measurements in continuous of a range of days. Thanks to all.

Answers (1)

Shruti Sapre
Shruti Sapre on 3 Sep 2015
Hi Cristina ,
I understand that you’re reading “.csv” files and they are not being read in order. It could be because of the sorting order of the operating system you are on, and that the files could be called into MATLAB in the incorrect order that you mention.
You could try to construct the filenames in a loop and pass that as an argument to make sure that a specific file is picked up while reading. This could ensure that files are read in the order that you desire. Perhaps you could use something like “sprint” to construct the file name, and append the directory path to the file name before passing it as an argument to the function that performs the read:
%num indicates the number of the file being read
fileName=sprintf('file%d.csv',num)
Hope this helps!
-Shruti

Products

Community Treasure Hunt

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

Start Hunting!