Possible to use string variable in plot title?
4 次查看(过去 30 天)
显示 更早的评论
I would like to create a string that can be used as part of a plot title.
Is this possible? I have searched documentation and have experimented but these efforts have yielded no helpful outcome
Thanks for any help
0 个评论
采纳的回答
Azzi Abdelmalek
2013-1-3
titlename='my plot'
title(titlename)
2 个评论
Jan
2013-1-8
Do you mean:
Data = rand(10);
newFileName = fullfile(filepath, [filename, '.processed']);
save(newFileName, 'Data');
更多回答(4 个)
José-Luis
2013-1-8
编辑:José-Luis
2013-1-8
This is a bit convoluted, and probably pointless, but it will get a string from a variable name.
Fz = rand(100,1);
fun = @(x) inputname(1); %Returns variable name, won't work for indexed variables
your_string = fun(Fz);
title(your_string); % or title(fun(Fz)); if you want it in a single line
A more elegant way would be to save your data as a structure, with one of the fields as the name and another with the actual data.
0 个评论
Jan
2013-1-8
It is a bad idea to let the name of the variable carry the information about its contents. The name of the variable should only depend on its functionality in the algorithm.
If a program is specific to a physical model, using a name, which reflects the real-world object, is helpful. But as soon as the program gets more abstract, the names of the variables should do this also. Example:
F = m * a
Here F is the force, m the mass and a the acceleration. For a simple physical simulation these terms are perfect. But when you write an ODE solver, these names can confuse the user, e.g. when some financial transactions are simulated.
In your case the "number crunching" seems to have the critical abstraction level already, such that afterwards "Fz" and "Fx" or whatever might be confused. Therefore it is worth to store such important information explicitly:
Data(1).name = 'Fz';
Data(1).value = rand(1, 1000);
Data(2).name = 'F_filtered';
Data(2).value = filter(B, A, rand(1, 1000));
Now even after extremely complicated programs, the required information is easy to obtain and the code can be expanded easily.
The general rule is to separate the values, the information about the physical meaning and the program itself. Then the abstraction allows to apply the program for more general purposes.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!