Is it possible to create a function for display variables with their names on figures?

For example I have variables: N = 1000; x = 10.5; y = -2.5; dt = 0.001; type = 'nonlinear' etc.
And I want a function with the following syntax:
display_on the_plot(N,dt,x,y,type) or display_on the_plot('N','dt','x','y','type')
which print the following text on the current plot:
'N = 1000, dt = 0.001, x = 10.5, y = -2.5, type = nonlinear'

답변 (4개)

Actually Thorsten, "That's impossible. If you pass the name, like display_in_plot('N', 'x'), the values of N and x are unknown inside the function. Likewise, the names are unknown if you pass the values, like in display_in_plot(N, x)."
Either way is actually possible and supported in matlab (even if it feels like a hack).
Passing the variable values, use inputname:
function str = generate_varstring_fromvalue(varargin)
%calling syntax:
%generate_varstring_fromvalue(N, dt, x, y, type)
for vidx = 1:nargin
vname = inputname(vidx);
vvalue = num2str(varargin{vidx});
str{vidx} = sprintf('%s = %s', vname, vvalue);
end
str = strjoin(str, ', ');
end
Passing the variable names, use evalin:
function str = generate_varstring_fromname(varargin)
%calling syntax:
%generate_varstring_fromname('N', 'dt', 'x', 'y', 'type')
for vidx = 1:nargin
vname = varargin{vidx};
vvalue = num2str(evalin('caller', vname));
str{vidx} = sprintf('%s = %s', vname, vvalue);
end
str = strjoin(str, ', ');
end
text(x, y, ['N = ' int2str(N) ', dt = ' num2str(dt) ', x = ' num2str(x) ', type = ' type])
where do you want that printed?
Have you considered using the builtin function "title"?
ttl = sprintf('N = %d, dt = %1.4f, x = %1.4f, y = %1.4f, type = %s',N,dt,x,y,type);
title(ttl);

댓글 수: 6

I would like a function doing this, not a script. The problem is that variables are passed and not its name. Just mention one of the nontrivial problem.
I would like to print it on the plot! for example into an annotation textbox, but is it possible to make nonediatble annotation textbox?
ok so you should go for Thorsten solution, i.e. use text.
You need to specify where on the plot. I assume (as Thorsten did) that x and y are where you want the label to appear. So
function h=display_on_the_plot(x,y,N,dt,type)
lbl = sprintf('N = %d, dt = %1.4f, x = %1.4f, y = %1.4f, type = %s',N,dt,x,y,type);
h=text(x,y,lbl);
h here is a handle to the label, in case you want to modify it later.
Uh wait. Do you mean that x, y, N etc could have different names, and you want your function to take that into account?
I'm not sure I understand.
Yes, I cannot know their names! That is the main problem!
@Mr M., then see my answer. Either of the two functions I've written will generate the text string for your plot. You then just need to insert the string in the plot with text.

댓글을 달려면 로그인하십시오.

Thorsten
Thorsten 2015년 4월 14일
편집: Thorsten 2015년 4월 14일
That's impossible. If you pass the name, like display_in_plot('N', 'x'), the values of N and x are unknown inside the function. Likewise, the names are unknown if you pass the values, like in display_in_plot(N, x).
An awful way to do it would be to declare all variables that the function has to process at any time as global outside the function, pass their names, as in the first example, declare them as global in the function using eval and then generate the string using another eval. You probably need another
eval(['x = whos' name ');'])
to determine the class of the variable (string or double or ...) to decide how to convert it to a string.
Overall, it seems that you can do something like you want, but it involves a lot of global declarations and eval, which is not a good programming style.
A better way would be to pass parameter, value pairs to the function, such as
display_in_plot('N', N, 'x', x, 'type', type)
if you insist on doing it with a function instead of using assembling the string w/o a function.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2015년 4월 14일

댓글:

2015년 4월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by