Reputation: 1989
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
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