MATLAB load not working with variable filename [duplicate]

Multi tool use
Multi tool use


MATLAB load not working with variable filename [duplicate]



This question already has an answer here:



I try to load a .mat file in MATLAB R2016a. However, when I make the filename variable, it fails with



Error using load
Unable to read file 'filename'. No such file or directory.



The documentation for R2018a states that the filename has to be



specified as a character vector or string



which i did. I searched for similar questions on SO, but they were all due to typing errors, e.g. Error using load; Unable to read file matlab



Code for reproduction:


clear all
mat1 = magic(5);
save mat1
clear mat1
load mat1 % working
clear mat1
filename = 'mat1.mat'; % tried with/without .mat
load filename % not working



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





Could the down-voters please give a reason? It took me some time to figure this out; it was not answered here before; it is encouraged to answer own questions: stackoverflow.com/help/self-answer
– David
Jun 29 at 10:55





I didn’t downvote, but this same question has been asked very often.
– Cris Luengo
Jun 29 at 12:26





Another one: stackoverflow.com/q/13692907/7328782
– Cris Luengo
Jun 29 at 12:33





Thanks for the response. I must have used the wrong search terms for not finding the first two examples. The third one seems to be a different problem.
– David
Jun 29 at 12:48




2 Answers
2



After further research I found that the documentation (R2018a) also states



load filename is the command form of the syntax. [...]
Do not use command form when any of the inputs, such as filename, are variables.



This answers my second question. Use:


load(filename)



The cause of this error is that the statement:


load filename



internally evaluates to:


load('filename.mat')



in order to support command-form statements statements like load mat1 in your example. It fails because the file filename.mat clearly does not exist.


load mat1


filename.mat



The function form is always safer, in my opinion.





Thanks for the response. However, if this is really the cause, something like filename = 'mat1';load filename (without .mat) should work - but it doesn't.
– David
Jun 29 at 12:56



filename = 'mat1';load filename





@David load filename evaluates to load('filename.mat'); not load('mat1.mat') and hence it shouldn't work.
– Sardar Usama
Jun 29 at 13:52



load filename


load('filename.mat')


load('mat1.mat')





The last sentence in the answer is opinion based though
– Sardar Usama
Jun 29 at 13:54





Yeah, @SardarUsama I made a change to that sentence. But is there any situation where the command form is safer?
– loudmummer
Jul 1 at 10:27





Right, okay. Understood.
– loudmummer
Jul 1 at 10:40

55HxOEqQEimbBniyX0JJtp,z1,I irmQrEFGf05bvDI,i0QgUp9 PuQsmxlAB
jRYWpjGaPP lzuiiJ42z,UH8hT,ECUlA5y6JlVDoyi4NHVfnMJuE Z0XvrqOSJ0MXZHZrBdd4

Popular posts from this blog

Rothschild family

Cinema of Italy