How to insert variable name into graph title
17 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dear All,
I would like to ask how I would print the variable name into the graph title
example:
function abc(varname)
.
.
.
title('Graph of 'insert varname here'')
thank you
0 commentaires
Réponse acceptée
Jan
le 15 Mar 2017
Modifié(e) : Jan
le 15 Mar 2017
The name of the variable should not matter. Not that it is "varname" in every case here. But the name might be different in the caller. Nevertheless, relying on the name is a pitfall. Better create a specific variable, which carries the name:
data = 1:10;
name = 'My Data';
function abc(data, name)
plot(data);
title(sprintf('Graph of %s', name));
But beside this general advice to separate the program and the data strictly, there is a dirty command to do this:
function abc(data)
name = inputname;
if isempty(name)
name = 'unknown';
end
plot(data);
title(sprintf('Graph of %s', name));
The test for the empyt name is required when you call your function e.g. by:
abc(1:10)
Then there is no name, because the data are created dynamically. The same problem:
x = 1:10;
abs(x(2:7))
I'm not sure if inputname works here. Therefore I would not play with such meta-programming techniques, when providing a specified name as additional variable is such dry and clean.
Perhaps you can use a struct:
data.value = 1:10;
data.name = 'Voltage at point X'
data.unit = 'mV'
You see, this offers many elegant ways to keep the data and the meaning packed together. And the name is not restricted to allowed names of variables.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!