xlsread with file names in arrays

8 views (last 30 days)
Saeid
Saeid on 15 Mar 2017
Answered: Guillaume on 15 Mar 2017
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 Comment
Guillaume
Guillaume on 15 Mar 2017
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?

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 15 Mar 2017
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

Community Treasure Hunt

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

Start Hunting!