print variable-name in Matlab

后端 未结 4 1782
执念已碎
执念已碎 2020-12-01 21:20

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 func

4条回答
  •  被撕碎了的回忆
    2020-12-01 21:59

    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.

提交回复
热议问题