在 Matlab 中打印变量名 [英] print variable-name in Matlab

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

问题描述

我在 Matlab 中有一个函数,其中有一些变量.我需要打印出 variable_names (以防万一等).我知道 inputname 函数,但它仅适用于 input_arguments.

mat = [ 1 2 ;3 4 ] ;% disp(['变量错误:' ??(a)])% 期望输出:var 中的错误:mat(而不是 1 2 ; 3 4!)

谢谢!

解决方案

Matlab 基本上不允许你这样做.但是,您可以编写一个辅助函数来减轻创建这样的输出的痛苦:

function disp_msg_var(msg, v)disp([msg inputname(2)]);结尾

在你的情况下你可以这样称呼:

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屋!

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