castagnaccio
castagnaccio

Reputation: 3

xlsread ('not the file name but a string contained in an element of an array that is the file name)

I would like to read an excel file (xlsread) but I don't want to put manually the string every time but instead I want to xlsread the name of the file that is contained in an array.

For example, my array B is:

B =
'john.xlsx'
'mais.xlsx'
'car.xlsx'

Then I would like to read the excel WITH THE NAME that is inside the first element, that means: "john.xlsx"

How can I do this?

Upvotes: 0

Views: 78

Answers (2)

scenia
scenia

Reputation: 1629

data = xlsread(B{1});

Or, if you want to read all of them:

for i=1:length(B)
    data(i).nums = xlsread(B{i});
end

Assuming, of course, your B is a cell array. If it's not, it can't exist the way you described it. If all strings have the same length (then it would be possible) or padding with spaces, you can split the char array into a cell array using

B = mat2cell(B,ones(size(B,1),1),size(B,2));

Upvotes: 1

Brian
Brian

Reputation: 617

Strings of different lengths would have to be inside a cell array, which you can access elements via the curly brackets {}. So, you can call xlsread on the first element this way:

names{1} = 'john.xlsx';
names{2} = 'mais.xlsx';
names{3} = 'car.xlsx';

num = xlsread(names{1});

Upvotes: 1

Related Questions