0

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)

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.