xlsread with file names in arrays

14 visualizaciones (últimos 30 días)
Saeid
Saeid el 15 de Mzo. de 2017
Respondida: Guillaume el 15 de Mzo. de 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 comentario
Guillaume
Guillaume el 15 de Mzo. de 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?

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 15 de Mzo. de 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!

Translated by