load('test.mat','y')
,当时用的这个命令,最初的想法是把test.mat的数据load到y中。然后就出现了报错:Warning: Variable 'y' not found
。
解决办法:我在command窗口输入doc load
,查看load('test.mat','y')
表示 Load only variable y from example file handel.mat. If the workspace already contains variable y, the load operation overwrites it with data from the file。即是从test.mat中下载变量y的值到workspace,也就是说y必须得是test.mat的变量才行。
补充:如果我们想实现最初的想法(把test.mat的数据load到y中),可以用 y = load('test.mat')
。此时y是一个结构体,假如要引用test.mat中的变量x_data,只需要用y.x_data
就可以引用了。
再补充:我们注意到(If the workspace already contains variable y, the load operation overwrites it with data from the file).如果要load两个.mat文件,而这两个文件中又相同的变量,后者是要替换到前者的,所以引用的时候,要提前把他重命名(赋值给一个新的变量)。