How to get Matlab to print both values and variable name
Show older comments
Is there any way to get Matlab to print both the name of the variables along with their values? I know the disp command shows the values without the variable name and the fprintf command allows you to print a string with variables. But I'm trying to sort variables from highest to lowest and I want to get Matlab to print their corresponding variables rather than just giving me numbers. thanks!
Accepted Answer
More Answers (2)
dpb
on 27 Nov 2017
Couple of choice depending upon just how you want to use it...
As a function, can retrieve the passed variable name with inputname so can write something like the following (without any error checking, formatting, etc., etc., etc., ...)--
function dispwithname(varargin)
% DISPWITHNAME(X) -- Display scalar variable(s) in argument list with name(s)
for i=1:nargin
disp([inputname(i) '= ' num2str(varargin{i})])
end
Alternatively, you could capture the list of desired variables from who with an argument list including appropriate wildcard and then iterate over that list having rearranged the order from the default aplphanumeric sort to the desired numeric order by use of the optional indexing array variable returned by sort
Improvements to format and the like are probably near endless...
1 Comment
Wojciech Kalinowski
on 27 Nov 2022
That's a realy good code. I edited the code for numbers so that the decimal point is alligned and I thought I might share it. Unfortunataly, it does not work well with string/chars now.
function dispwithname(varargin)
% DISPWITHNAME(X) -- Display scalar variable(s) in argument list with name(s)
% finds the largest number before the decimal point
maxChar = 0;
bks2 = zeros(nargin,1); % number of blank space to align the decimal points
for i = 1:nargin
a1 = textscan(num2str(varargin{i}), '%s %s', 'delimiter','.');
bks2(i) = strlength(string(a1(1)));
if bks2(i) > maxChar
maxChar = bks2(i);
end
end
% displays the numbers aligned at the decimal point
for i=1:nargin
bks1 = blanks(4-length(inputname(i))); % number of blank spaces
disp([' ' inputname(i) bks1 ' = ' blanks(maxChar-bks2(i)) num2str(varargin{i})])
end
end
There probably is a more efficent way to do this.
example:
a = -123.0258;
b = 123.213521;
c = 128546.3;
d = -2354.213;
e = 0;
dispwithname(a,b,c,d,e);
output:
a = -123.0258
b = 123.2135
c = 128546.3
d = -2354.213
e = 0
Image Analyst
on 27 Nov 2017
Just put the name of the variable on the line of code without anything else, no semicolon, etc. For example if your variable is myVar, DO NOT DO
disp(myVar);
DO this instead
myVar
2 Comments
kami
on 27 Nov 2017
dpb
on 27 Nov 2017
Sure...
>> data=randn(10,5); % dummy valuess
>> lake_Erie = mean(data(:,1));
lake_Huron= mean(data(:,2));
lake_Michigan = mean(data(:,3));
lake_Ontario = mean(data(:,4));
lake_Superior= mean(data(:,5));
>>dispwithname(lake_Erie,lake_Huron,lake_Michigan,lake_Ontario,lake_Superior)
lake_Erie= -0.50798
lake_Huron= -0.10307
lake_Michigan= 0.018442
lake_Ontario= -0.29129
lake_Superior= 0.4192
>>
using the routine I showed earlier.
But, you don't even need to do that if use the alternate version I mentioned--
>> clear lake_names % get rid of one we don't want/need...
>> vnames=who('lake_*'); % get the list of variables of interest
>> for i=1:length(vnames)
fprintf('%s=%f\n',vnames{i},eval(vnames{i}))
end
lake_Erie=-0.507984
lake_Huron=-0.103066
lake_Michigan=0.018442
lake_Ontario=-0.291291
lake_Superior=0.419201
>>
Again, clean up format and other details to heart's content...
Categories
Find more on Multidimensional Arrays 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!