xlsread with file names in arrays

1 次查看(过去 30 天)
Saeid
Saeid 2017-3-15
回答: Guillaume 2017-3-15
I have 3 excel files with following names
'Input1.xls'
'Input2.xls'
'Input3.xls'
Is there a way to define an array such as:
InputFiles={'Input1.xls' 'Input2.xls' 'Input3.xls'}
so that I can write a loop where each file name is called based on its index:
for i=13
datax=xlswrite(InputFiles(i))
end
I try this exactly as i just wrote but for some reason Matlab doesn't accept it.
  1 个评论
Guillaume
Guillaume 2017-3-15
It would help if you typed your example correctly,
I assume
for i = 13
is meant to be
for i = 1:3
and
data=xlswrite(...
is meant to be
data=xlsread(...
In what way matlab does not accept your code?

请先登录,再进行评论。

回答(1 个)

Guillaume
Guillaume 2017-3-15
Other than the many typos and the wrong brackets used to index the cell array, the main issue I can see with your code is that you constantly overwrite the same output, so at the end of the loop, you're only left with the content of the last file. This would fix this:
InputFiles = {'Input1.xls', 'Input2.xls', 'Input3.xls'};
Data = cell(size(InputFiles)); %preallocate output cell array for efficiency
for i = 1 : numel(InputFiles) %don't hardcode the end of the loop when you can just ask matlab for the value
Data{i} = xlsread(InputFiles{i}); %use {} to access the content of a cell
end

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by