Possible to use string variable in plot title?

4 views (last 30 days)
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

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 3 Jan 2013
titlename='my plot'
title(titlename)
  2 Comments
Don
Don on 8 Jan 2013
Thank you very much for your help. It gets me part way to the solution.
I'm writing a script and use these use commands:
[filename, filepath] = uigetfile('*.edf', 'Select edf file'); to select a file, then
fname = [filepath filename]; [hdr, record]=edfRead(fname);
That gets me several arrays of size 1X20736, and I give them names, e.g.,
Fz = record(19,:);
By now, Fz is associated with an vector of numbers.
Now my problem: after considerable number crunching, I plot some results and want the script to put the name of the selected variable , e.g., Fz, into the plot title. I will be grateful for suggestions how to do this. When I test these commands at the command line, titlename='my plot' title(titlename) titlename=Fz gets me a string of numbers and titlename='Fz' get the result I want but I can't see how to do that within the script when 'Fz' cold be any of the variable names
A second and perhaps related issue is how to save a dataset of calculated results , RatioResults, using the filepath obtained above. I can't find how to use save (or write or export) to do this. I could resort to uisave and require another operation by the user. can you suggest a solution for this also?
Many many thanks, in advance
Don
Jan
Jan on 8 Jan 2013
Do you mean:
Data = rand(10);
newFileName = fullfile(filepath, [filename, '.processed']);
save(newFileName, 'Data');

Sign in to comment.

More Answers (4)

venu gopal
venu gopal on 8 Dec 2020
title ("sample mass")

José-Luis
José-Luis on 8 Jan 2013
Edited: José-Luis on 8 Jan 2013
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.

Jan
Jan on 8 Jan 2013
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.

venu gopal
venu gopal on 8 Dec 2020
title (" sample mass")

Categories

Find more on 2-D and 3-D Plots 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!