teaLeef
teaLeef

Reputation: 1989

Loading files in a for loop in matlab

I am trying to load different file names contained in a matlab vector inside a for loop. I wrote the following:

fileNames = ['fileName1.mat', ..., 'fileName_n.mat'];
for i=1:n
    load(fileNames(i))
    ...
end

However, it doesn't work because fileNames(i) returns the first letter of the filename only.

How can I give the full file name as argument to load (the size of the string of the filename can vary)

Upvotes: 0

Views: 269

Answers (1)

Nemesis
Nemesis

Reputation: 2334

Use a cell instead of an array.

fileNames = {'fileName1.mat', ..., 'fileName_n.mat'};

Your code is in principle a string cat, giving you just one string (since strings are arrays of characters).

for i=1:n
    load(fileNames{i})
    ...
end

Use { and } instead of parentheses.

Upvotes: 1

Related Questions