Read an excel file from excel file

조회 수: 1 (최근 30일)
KRUNAL
KRUNAL 2014년 8월 6일
댓글: Iain 2014년 8월 7일
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.

답변 (1개)

Iain
Iain 2014년 8월 6일
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
KRUNAL
KRUNAL 2014년 8월 6일
편집: KRUNAL 2014년 8월 6일
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 2014년 8월 7일
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');

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by