在Matlab中动态分配变量 [英] Dynamically Assign Variables in Matlab

查看:179
本文介绍了在Matlab中动态分配变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为继承的大型代码库的一部分,我具有以下功能:

I have the following function as part of a large codebase, which I inherited:

function = save_function(fpath, a,b,c)
    save(fpath, 'a', 'b', 'c')
end

在执行另一个脚本之前,在一个脚本的末尾调用此函数.这样,变量名就被正确保存了(我知道设计不好-我没有写这段代码).

This function is called at the end of one script, before another script is to be executed. This way, the variable names are properly saved (bad design, I know - I didn't write this code).

现在,我正在对代码库进行更改,并意识到我需要在fpath中存储更多变量.我面临两个选择:

Now, I am making changes to the codebase, and realize that I need to store more variables in fpath. I face two options:

  1. 编辑save_function以接受更多输入.这会破坏代码库中也使用此功能的所有其他代码
  2. 写一个save_function2(a, b, c, d, e, ...),我将在更改的代码中调用它.这似乎也很糟糕.
  1. Edit save_function to accept more inputs. This would break any other code in the codebase that also uses this function
  2. Write a save_function2(a, b, c, d, e, ...) that I will call in the code that I change. This seems like bad design as well.

理想情况下,我希望允许save_function一次接受任意数量的参数,并通过传入的变量名称保存所有参数.

What I would ideally like to do, is to allow save_function to take in any number of arguments at a time, and save them all by the variable names that are passed in.

进行了一些谷歌搜索,我发现了evaleval_in,它们将字符串评估为matlab代码.但是,使用此方法有两个问题:

Having done some googling, I found eval and eval_in, which evaluate strings as matlab code. However, there are two problems with using this:

  1. 使用eval的速度非常慢,而且非常危险
  2. 我并不总是事先知道变量的类型,所以我无法创建一个优雅的,通用的to_string函数
  1. Using eval is horribly slow and quite dangerous
  2. I don't always know the types of my variables beforehand, so I can't create an elegant, generic to_string function

为了与可变数量的变量作斗争,我决定按以下方式使用varargininputname:

In order to combat the flexible number of variables, I decided to use varargin and inputname as follows:

function = save_function(fpath, varargin)
    names = {}
    for i=1:size(varargin,1)
        names{i} = inputname(i+1);  % have to offset by 1 to account for fpath
    end
    save(fpath, names{:});
end

不幸的是,由于输入变量保存在varargin中,因此它们不作为变量名存在于堆栈中,因此save行失败

Unfortunately, since the input variables are held in varargin, they do not exist as their variable names on the stack, so the save line fails

如何使用变量名在堆栈上动态创建这些变量?

How can I dynamically create these variables on the stack, with their variable names?

推荐答案

您可以使用结构来动态定义保存的变量名.
此处.

You can use a structure to dynamically define saved variable names.
This option is documented here.

 function save_function( fpath, varargin )     
 for ii = 1:numel( varargin )
     st.( inputname(ii+1) ) = varargin{ii};
 end
 save( fpath, '-struct', 'st' );

根据经验,结构对于动态变量名称,使用动态字段名称通常比evalassignin更好.

As a rule of thumb, structure with dynamic field names is often better than eval or assignin when it comes to dynamic variable names.

PS,
最好不要在Matlab中使用i作为变量名.

PS,
It is best not to use i as variable name in Matlab.

这篇关于在Matlab中动态分配变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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