Import, sort and extract multiple excel files into 1 table/vector

6 views (last 30 days)
Hi I have 12 .csv files (1 for each month) with a fair amount of data in each, although they are presented the same way. I want to bring in all 12 files into MATLAB, and store the same 3 columns of data (columnns 2-4, rows 6-36(or end)) from each file sequentially in 3 vectors date, min, max. I have been researching and struggling all morning, and the below code is the best I have
% tell MATLAB to look for all .csv files in currently directory
files15 = dir('*.csv');
% Loop the same amount of times as number of files
for i = 1:numel(files15)
% store the file name in a column cell
x(:,i) = double(files15(i).name)
% Set a placeholder inside the file name to remove string
fNum = find((x),'TEST')
% Extract the number from the file name
out(:,i)=(fNum(fNum+5:end-4))
% sort files numerically 1-12
neworder = sort(out)
% import numbers and text as separate components for each file
[import15,import15t] = xlsread (files15);
% Store all dates in one column
d6 = import5t(7:end,2);
% Store all min and max temperatures in two columns
Tmin6 = import5(1:end,1);
Tmax6 = import5(1:end,2);
end
I think I am making it more complicated than it needs to be, any suggestions on the simplest way to achieve this?
Thanks in advance Anthony
  4 Comments
dpb
dpb on 20 Aug 2018
You'd have to modify the sscanf line a little in my posting; didn't mention about the YYYY- string before. Easy enough to do use
fmt='TEST %d-%d.csv';
and can return both values.
As a way to avoid all this grief about order, any time you create files sequentially such as this, use a format when creating the file names that includes the preceding '0's in the number fields so that the files are sorted numerically as well as lexically automagically --
fn=sprintf('TEST %d-%02d.csv',year,mo);
will produce
>> fn=sprintf('TEST %d-%02d.csv',2017,8)
fn =
'TEST 2017-08.csv'
>>
etc., and then dir will return a list of files sorted in the order desired already.
Anthony Cook
Anthony Cook on 20 Aug 2018
Thank you for your help. I agree with just renaming the files to be in order, but not allowed in this case :) Thanks again

Sign in to comment.

Accepted Answer

dpb
dpb on 20 Aug 2018
Good start but have made it a little more complicated than need...to get the files in numerical instead of lexical order
d=dir('*.csv');
[~,ix]=sort(cellfun(@(s) sscanf(s,'TEST%d.csv'),cellstr({d.name})));
d=d(ix);
Now, as IA suggests, it would be good to know what the format of the files actually is; I'm guessing there are 6 header lines from the indexing expression for the dates so something like
dates=[];
data=[];
for i=1:length(d)
% import numbers and text as separate components for each file
[v,t] = xlsread(d(i).name);
dates=[dates;t(7:end,2)];
data=[data;v];
end
Probably this would be a good application for a table and readtable, but need the details of the data structure.
  5 Comments
dpb
dpb on 21 Aug 2018
Glad to help...just happened to notice the comment and dawned on me you may have not understood the underlying cause of the failure.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!