问题描述
我想处理在文件中找到的每个实例(我发现使用 whos(' - file'...
)我希望 load
能让我指定一个变量的目的地名称,这样我就不必担心冲突了(所以我不需要写一个la eval
的自修改代码) 。
蛮力的做法似乎是创建一个帮助函数,使用名字变量,希望不会与.mat内容冲突,例如:
- 文件中的
whos
来获取包含的名称。 / li>
- 使用
eval
来加载每个包含的结构。 ,一个单元格数组(其中一列数组包含.mat文件的结构名称和一个对应的列w从.mat文件中获得每个结构的实际内容)。
没有更好的方法来完成同样的事情吗?
/ p> - Does a
whos
on the file to get the contained names. - Iteratively load each contained structure.
- Uses
eval
to assign the loaded structure into, say, a cell array (where one column of the array contains the .mat file's structure names and a corresponding column with the actual contents of each structure from the .mat file).
当然可以!只要使用 load
和一个输出参数即可。
x = 1;
save foo;
ls = load('foo.mat');
ls.x
Say I have a .mat file with several instances of the same structure, each in a different variable name.
I want to process each instance found in a file (which I find using whos('-file' ...
). I was hoping that load
would let me specify the destination name for a variable so that I didn't have to worry about collisions (and so that I didn't have to write self-modifying code a la eval
).
The brute force way to do this appears to be creating a helper function that, using variables with names that hopefully don't conflict with the .mat contents, does something like:
Is there no more elegant way to accomplish the same thing?
Of course you can! Just use load
with an output argument.
x = 1;
save foo;
ls = load('foo.mat');
ls.x
这篇关于从一个.mat文件加载一个变量到一个不同名称的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!