¿How can I get the value of a variable that is inside a function?

2 views (last 30 days)
I would like to know how I can get the value of matrix A (random) on the command window without doing this:
function []=a123displayingmatrixA()
A=random(5); A
end
I don't want the function to receive or to return anything, because I don't want to work with matrix A as a variable, I just want the function to display it on the command window. I could use display, but that doesn't give me what the name of the variable is; this is something very important when I have to know about many variables and not just one. Also, if I simply write 'A' without the semicolon, I receive the message 'line k: terminate statement with semicolon to suppress output (in functions)'. I don't like this option because I don't want to have orange warnings stating something that I already know. I want the orange warnings to appear when I really need them to. That's why I don't want to change the Code Analyzer Message Indicators.

Accepted Answer

dpb
dpb on 27 Feb 2016
Since you have to write the specific variable anyway, I'd just add the name--something like
...
A=random(5);
>> A=randn(3);
>> disp('A');disp(A)
A
1.4172 0.7172 1.0347
0.6715 1.6302 0.7269
-1.2075 0.4889 -0.3034
>>
If you're doing this a lot you could write a little utility routine that does the above as
>> type dispname
function dispname(x)
disp(inputname(1))
disp(x)
>> dispname(A)
A
1.4172 0.7172 1.0347
0.6715 1.6302 0.7269
-1.2075 0.4889 -0.3034
>>
  2 Comments
Ricardo Boza Villar
Ricardo Boza Villar on 27 Feb 2016
That's a good point. Thanks for solving my problem, though I think Matlab should have its own function to do that, not just disp().
dpb
dpb on 27 Feb 2016
Guess can always ask for an enhancement request to disp to add a flag which would be an indicator for displaying the array name...the above seems simple enough, however.

Sign in to comment.

More Answers (2)

Stephen23
Stephen23 on 27 Feb 2016
Edited: Stephen23 on 27 Feb 2016
Those warning messages are extremely simple to get rid of, either individually (per line), per file, or as a MATLAB setting. It seems that you are basically wish to have this display the variable name and value, but without the warning message:
A = random(5)
Suppressing the warning message is easy:
A = random(5) %#ok<NOPRT>
It will also print the desired information, but does not show any warning message. This is much simpler than creating some custom function. Adding these suppression messages is easy to do yourself:
  • right click the orange warning.
  • select Suppress "..."
  • choose where you want to suppress.
Or in pictures, for those who are visual learners:
Right click:
Select "Suppress "...":
Bingo, no warning!:
  3 Comments
Stephen23
Stephen23 on 27 Feb 2016
Edited: Stephen23 on 27 Feb 2016
"that seems like a lot of effort to have to go thru on a per line basis": I don't see how this is much effort at all. I accept/reject these warning messages as soon as I write a line of code, so that effort is insignificant compared to the code writing itself.
My answer is simply based on the KISS principal: I would not waste my time re-inventing some functionality that already basically exists and is debugged, robust, and well documented. The time spent on developing this function is already much larger than inserting a few warning suppression statements would have taken.
dpb
dpb on 27 Feb 2016
"...time spent on developing this function is already much larger than inserting a few error suppression statements would have taken"
For a one-time file, sure. If it's a normal, repetitive action by the OP in his usage, over time, "not so much". I guess others use the editor in much different fashion than I; I pretty much code and ignore stuff like that... :) Although, truthfully, I really don't write code in the ML editor; it's just too different from the one with which I've gotten used to over the years so I normally only see a piece of code in it after there's already quite a lot if I were to use the debugger or somesuch. Hence, it'd be quite a number to look at at a time...not that that's necessarily a recommendation, just me. :)

Sign in to comment.


Ricardo Boza Villar
Ricardo Boza Villar on 27 Feb 2016
That's right, it seems like a lot of effort (relatively, of course ;)). Thanks for contributing with another point of view, anyway.
I'd like to ask something else. Following dpb's idea, I have this:
function []=dsp(varargin)
n=nargin;
for i=1:n
fprintf(inputname(i)), disp(' ='), disp(input(i))
end
% Copy and paste the text below where needed:
% Everything above comes with the function:
% function []=dsp(n)
% fprintf(inputname(1)), disp(' ='), disp(n)
% end
% In order to display variable values without treating them as variables or
% receiving warning messages such as: 'line k: Terminate statement with
% semicolon to suppress output'.
Ignore the commentary. I'd like to know how to refer to the input arguments by a number (as if they were part of a vector), so the script I have written works. It doesn't work properly because of the 'disp(input(i))' part. I'd like the answer to this particular question, but I welcome other approaches.
  3 Comments
dpb
dpb on 28 Feb 2016
Edited: dpb on 28 Feb 2016
"That's right, it seems like a lot of effort..."
Actually, I kinda' like it!!! It didn't take any time to speak of to write and it is kinda' a handy debug tool to have the formatting with the identification. Consequently, I have copied it over into my "Utilities" subdirectory in which I keep a whole slew of such little goodies. As you've extended, it could be made more general and have some additional facilities as well...plus, if nothing else it illustrates some of the lesser-known/used Matlab features.
So, I think it's precisely the kind of customization Matlab fosters to easily create such additional functionality...sure, it's just "syntactic sugar" as you can get the same thing with the two lines or as Stephen argues either fixup the warnings or as I would ignore them but why not have what you really want?
I'd be more in Stephen's camp if there were a way to automagically turn off warnings for a subset of the warning type within the general as "if it is just a variable name on the line, then I don't want to be warned but if it is a code line, then I do". Lacking that and having to either kill 'em all or do it manually I agree isn't user friendly; 'puters should be tools, not bosses.

Sign in to comment.

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!