'load' function bug?

4 次查看(过去 30 天)
Eugenio
Eugenio 2012-3-8
Hi all, these days I'm writing a function that take a strings as input, let call it 'var'; that string contain the path to a .mat file conteining a variable called 'mat'(it'a a NxM matrix). After I need to load the matrix saved into the .mat file, so i use the command
...
load 'var'
...
and the variable 'mat' appears on my workspace. Instead, if i use a different name for the input variable, for example 'var_1', or 'm_1' or something, it doesn't work!
...
??? Error using ==> load
Unable to read file m_1: No such file or directory
..
The code below is working
...
prova='C:filename.mat';
...
load 'prova'
inside my workspace I found tha matrix 'mat'
But if i rename the variable containing tha variable name as below
...
n=prova;
load 'n'
??? Error using ==> load
Unable to read file n: No such file or directory
...
Can anybody explain me this?! It's so annoyng because my function works only depending on the name of the input variable!
  1 个评论
Jan
Jan 2012-3-8
Btw. It is recommended not to use "var" as name of a variable, because this shaddows the built-in function VAR. Try this:
clear var
var(1:10)
var = rand(1, 10)
var(1:10)

请先登录,再进行评论。

采纳的回答

Jan
Jan 2012-3-8
Please read the documentation of load and save again. Look in the given examples to learn the difference between these possibilities
name = 'myfile'
save 'name' % saves "name.mat"
save name % saves "name.mat"
save(name) % saves "myfile.mat"
save('name') % saves "name.mat"
The non-functional form save name or the equivalent save 'name' is useful in the command window, but it confuses beginners frequently.

更多回答(1 个)

Walter Roberson
Walter Roberson 2012-3-8
load(prova)
and correspondingly
save(prova)
I think if you check you will find that you happen to have a file named var.mat
  2 个评论
Eugenio
Eugenio 2012-3-8
Sorry Walter, can you explain what you mean?
Walter Roberson
Walter Roberson 2012-3-8
Do not use
load 'prova'
as your syntax. Instead, use
load(prova)
as your syntax.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by