在Matlab中打印变量名 [英] print variable-name in Matlab
问题描述
我在Matlab中有一个函数,其中包含一些变量.我需要打印出variable_names(在发生异常等情况下).我知道inputname
函数,但是它仅适用于input_arguments.
mat = [ 1 2 ; 3 4 ] ;
% disp(['Error in var: ' ??(a)])
% desired ouput: Error in var: mat (and NOT 1 2 ; 3 4!)
谢谢!
Matlab本质上不允许您这样做.但是,您可以编写一个辅助函数来减轻创建这样的输出时的麻烦:
function disp_msg_var(msg, v)
disp([msg inputname(2)]);
end
在您的情况下可以这样称呼:
disp_msg_var('Error in: ', a);
您可以在 Mathworks论坛上阅读有关该主题的更多讨论 >
此外,要列出所有具有值的当前变量,可以使用who
函数,但这不是您提出的问题.
I have a function in Matlab that has some variables in it. I need to print out the variable_names (in case of an exception etc.). I am aware of inputname
function but it works for input_arguments only.
mat = [ 1 2 ; 3 4 ] ;
% disp(['Error in var: ' ??(a)])
% desired ouput: Error in var: mat (and NOT 1 2 ; 3 4!)
Thanks!
Matlab essentially does not let you do that. However, you can write a helper function to ease your pain in creating output like that:
function disp_msg_var(msg, v)
disp([msg inputname(2)]);
end
which you could call like so in your case:
disp_msg_var('Error in: ', a);
You can read more discussion on the topic on the Mathworks forum
Additionally, to list all current variables with values you can use the who
function, but that is not the problem you presented.
这篇关于在Matlab中打印变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!