Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
generate plot titles from character array

Subject: generate plot titles from character array

From: blubb

Date: 28 Sep, 2010 17:53:05

Message: 1 of 4

hi,

i have argument-names saved as an character array.
i plotted some data and now i want to the names of the arguments (saved in an array) as plot titles.

at the moment my code looks like this schematically:

for i=1:N
varnames(:,i) = cellstr(txt(:,i));
plot(X(:,i),Y(:,i));
title(['print -dtiff plot_' num2str(i)]); % but i prefer the argument name as the plot title instead of only the number of the counter i. how do i get matlab to use the names saved in txt to use as a plot title?

thanks in advance for advice on this!

Subject: generate plot titles from character array

From: Sean

Date: 28 Sep, 2010 18:16:20

Message: 2 of 4

"blubb " <blubb@trash-mail.com> wrote in message <i7ta21$s83$1@fred.mathworks.com>...
> hi,
>
> i have argument-names saved as an character array.
> i plotted some data and now i want to the names of the arguments (saved in an array) as plot titles.
>
> at the moment my code looks like this schematically:
>
> for i=1:N
> varnames(:,i) = cellstr(txt(:,i));
> plot(X(:,i),Y(:,i));
> title(['print -dtiff plot_' num2str(i)]); % but i prefer the argument name as the plot title instead of only the number of the counter i. how do i get matlab to use the names saved in txt to use as a plot title?
>
> thanks in advance for advice on this!

Have the argument names stored in a cell array, then reference it:

arg_titles = cell_array_of_titles
for ii = 1:N
   figure;
   plot(stuff);
   title(arg_titles{ii});
end
   

Subject: generate plot titles from character array

From: Walter Roberson

Date: 28 Sep, 2010 18:16:32

Message: 3 of 4

On 10-09-28 12:53 PM, blubb wrote:

> i have argument-names saved as an character array.
> i plotted some data and now i want to the names of the arguments (saved
> in an array) as plot titles.
>
> at the moment my code looks like this schematically:
>
> for i=1:N
> varnames(:,i) = cellstr(txt(:,i));
> plot(X(:,i),Y(:,i));
> title(['print -dtiff plot_' num2str(i)]); % but i prefer the argument
> name as the plot title instead of only the number of the counter i. how
> do i get matlab to use the names saved in txt to use as a plot title?

To double check to be sure we are talking about the same thing: currently your
['print -dtiff plot_' num2str(i)] expression inside of your title() call will
simply construct a character string such as 'print -dtiff plot_1' . I can't
think of any reason you would want to use that particular string, which looks
so much like a matlab command, unless your intention is to remind the user
what they have to do to print that particular plot -- but if that were the
case, then it would seem to make more sense to add a control to the plot to
request printing .


cellstr() returns a cell array, with the expectation that the input to it is a
character array. If txt(:,i) does not happen to be a cell array, then it is
going to result in a column vector of text, and cellstr() of that would result
in a cell array containing strings each of one character. That doesn't sound
like it would be what you want... at the very least it would not be
generalizable to multi-character variable names.

Because cellstr() returns a cell array, if varnames() was not pre-allocated,
then by the end of the loop, varnames would end up being a 1 x N cell array.

Your question says that you prefer "the" argument name, but your varnames
logic is constructing a column vector of single-character strings, making it
questionable as to which one is "the" argument name for your purposes.

Your code would make more sense if your txt(:,i) reference were txt(i,:)
If that were the case, then I would replace that line with

varnames{i} = deblank(txt(i,:));

and then

title(varnames{i})

or something similar, such as

title(['pH 30 minutes after adding 7 mol of ', varnames{i}, ...
' to Cherry Garcia ice cream']);

Subject: generate plot titles from character array

From: blubb

Date: 29 Sep, 2010 08:17:23

Message: 4 of 4

thank you walter,

varnames{i} = deblank(txt(i,:));
title(varnames{i});

worked perfectly!

Walter Roberson <roberson@hushmail.com> wrote in message <i7tbj6$lvp$1@canopus.cc.umanitoba.ca>...
> On 10-09-28 12:53 PM, blubb wrote:
>
> > i have argument-names saved as an character array.
> > i plotted some data and now i want to the names of the arguments (saved
> > in an array) as plot titles.
> >
> > at the moment my code looks like this schematically:
> >
> > for i=1:N
> > varnames(:,i) = cellstr(txt(:,i));
> > plot(X(:,i),Y(:,i));
> > title(['print -dtiff plot_' num2str(i)]); % but i prefer the argument
> > name as the plot title instead of only the number of the counter i. how
> > do i get matlab to use the names saved in txt to use as a plot title?
>
> To double check to be sure we are talking about the same thing: currently your
> ['print -dtiff plot_' num2str(i)] expression inside of your title() call will
> simply construct a character string such as 'print -dtiff plot_1' . I can't
> think of any reason you would want to use that particular string, which looks
> so much like a matlab command, unless your intention is to remind the user
> what they have to do to print that particular plot -- but if that were the
> case, then it would seem to make more sense to add a control to the plot to
> request printing .
>
>
> cellstr() returns a cell array, with the expectation that the input to it is a
> character array. If txt(:,i) does not happen to be a cell array, then it is
> going to result in a column vector of text, and cellstr() of that would result
> in a cell array containing strings each of one character. That doesn't sound
> like it would be what you want... at the very least it would not be
> generalizable to multi-character variable names.
>
> Because cellstr() returns a cell array, if varnames() was not pre-allocated,
> then by the end of the loop, varnames would end up being a 1 x N cell array.
>
> Your question says that you prefer "the" argument name, but your varnames
> logic is constructing a column vector of single-character strings, making it
> questionable as to which one is "the" argument name for your purposes.
>
> Your code would make more sense if your txt(:,i) reference were txt(i,:)
> If that were the case, then I would replace that line with
>
> varnames{i} = deblank(txt(i,:));
>
> and then
>
> title(varnames{i})
>
> or something similar, such as
>
> title(['pH 30 minutes after adding 7 mol of ', varnames{i}, ...
> ' to Cherry Garcia ice cream']);

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us