Read an excel file from excel file

3 views (last 30 days)
KRUNAL
KRUNAL on 6 Aug 2014
Commented: Iain on 7 Aug 2014
I have an excel file in which one of the column contains the names of another excel files. What I am trying to do is 1st using xlsread opening the file that contains the list in and then in a loop I am trying to open each of these excel files one by one,do some calculations and show its result in the row where its file name is stored. My two files looks like this :
1st file :
Origin Date (Local) Origin Time (Local) File name
file1
file2
file3
2nd file (file1):
OriginTime Year Month Day Hour Minute Second
51.65086761 2014 6 10 12 9 5.991625
53.03222393 2014 6 10 12 9 5.991625
52.47999444 2014 6 10 12 9 5.991625
52.55764572 2014 6 10 12 9 5.991625
52.73042468 2014 6 10 12 9 5.991625
and similar data in file2 and file3.
I did start a little but I don't know where I am wrong in the code below:
orginf = '1st file location';
destfold = 'folder containing the three excel files...file1,file2,file3';
for n = 1:5
sheet = 'Sheet1';
xlRange = ['U' num2str(n+1)];
[~,namefile] = xlsread(orginf,sheet,xlRange);
sfile = strcat(destfold,namefile,'.xlsx');
[~,~,ofile] = xlsread(sfile);
It says error in line xlsread(sfile)...filename must be a string.

Answers (1)

Iain
Iain on 6 Aug 2014
namefile is a cell array. You need to address it like this:
namefile{3,5}
You'll need to check if the file exists, if there is a file name actually there, etc.
  2 Comments
KRUNAL
KRUNAL on 6 Aug 2014
Edited: KRUNAL on 6 Aug 2014
What about xlRange ? I mean its different that in my file the name of the three excel files are stored in U column. If I am keeping it the same it is giving me a 3x5 matrix of cells with each one having values as "[ ]"
Iain
Iain on 7 Aug 2014
This is what I'd do:
[numbers text raw] = xlsread(orginf, sheetno); % read the whole sheet.
column = 21; % U is the 21st letter of the alphabet isn't it?
for i = 1:size(raw,1) % for each column...
a = dir(raw{i,column});
if numel(a)
[~,~,ofile] = xlsread(raw{i,column});
... do stuff
end
end
namefile{3,5}, now I've realised that you're reading out a single cell at a time would be wrong, it would be:
sfile = strcat(destfold,namefile{1,1},'.xlsx');

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!