从一个.mat文件加载一个变量到一个不同名称的变量 [英] loading a variable from a .mat file into a differently named variable

查看:184
本文介绍了从一个.mat文件加载一个变量到一个不同名称的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想处理在文件中找到的每个实例(我发现使用 whos(' - file'... )我希望 load 能让我指定一个变量的目的地名称,这样我就不必担心冲突了(所以我不需要写一个la eval 的自修改代码) 。

蛮力的做法似乎是创建一个帮助函数,使用名字变量,希望不会与.mat内容冲突,例如:


  1. 文件中的 whos 来获取包含的名称。 / li>
  2. 使用 eval 来加载每个包含的结构。 ,一个单元格数组(其中一列数组包含.mat文件的结构名称和一个对应的列w从.mat文件中获得每个结构的实际内容)。


    没有更好的方法来完成同样的事情吗?

  3. / p>

    解决方案

    当然可以!只要使用 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:

    1. Does a whos on the file to get the contained names.
    2. Iteratively load each contained structure.
    3. 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).

    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文件加载一个变量到一个不同名称的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆