Is any change 'DisplayName' property in plot function?

plot(x,y,'DisplayName',{'name1','name2','name3','name4'})
produce error
Error using plot
Value must be a string.
This shall produce this plot with curve names.
Is any change in 'DisplayName' property in plot function?

1 commentaire

Using 2020a
Names need to be a char:
plot(magic(4),'DisplayName',char({'name1','name2','name3','name4'}))
You still need to specify a minimum the legend afterwards to display it, eg:
legend('location', 'best');

Connectez-vous pour commenter.

 Réponse acceptée

When we were rebuilding all of the graphics objects as MATLAB Objects, we found they they had several different variations of code that parsed this cell array case. You'll find in older versions of MATLAB that some variations of this will work for some objects, but not for others.
We wanted to unify this code so that all of the objects worked the same way. That way you wouldn't have to learn the differences between the different objects. But we couldn't come up with one version that replicated all of the existing variations. This particular case was one of the casualties.
The simplest workaround here is to use the set command, because it works the same for all objects. In this case, the following should work in any version of MATLAB.
h = plot(magic(3));
set(h,{'DisplayName'},{'first';'second';'third'})
legend show
If you look carefully at this, you'll notice some important points.
  • The property name is also in a cell array.
  • The cell array of values is the same shape as the array of handles.
This is the canonical form of this cell array 'dealing' syntax. It's the one that the various plotting commands were trying to match. But most of them were slightly different from this.

5 commentaires

Jan Buytaert
Jan Buytaert le 5 Août 2015
Modifié(e) : Jan Buytaert le 5 Août 2015
Dear Mike, Thank you for clarifying this. I however strugle with the following three problems:
1/ Your code combined in the snippet below does not work
h = plot(magic(3),{'DisplayName'},{'first';'second';'third'});
2/ Also the code below behaves unexpected: a shift between the two plots.
h = plot(magic(3));
hold on
plot(h,magic(3));
3/ Adding Displaynames is also a problem ...
h = plot(magic(3));
set(h,{'DisplayName'},{'first';'second';'third'})
legend show
hold on
plot(h,magic(3))
set(h,{'DisplayName'},{'first';'second';'third';'first';'second';'third'})
Note that in 3/ their is no shift between the plots as in 2/ !?
Could you provide any help on these three problems?
1) I guess I wasn't clear. I was saying that this:
h = plot(magic(3),{'DisplayName'},{'first';'second';'third'});
would not work. You need to use the set command.
2) I'm not clear on what you expected this to do:
h = plot(magic(3));
hold on
plot(h,magic(3));
The first call to plot is returning the handles of the three graphics objects. You've passed those into the second call to plot as the X coordinates. It didn't error because of a tricky bit of code which is in there for compatibility with old versions of MATLAB where handles were doubles. It found that it could cast the handles to doubles because of that, and it used those double handles as the X coordinates. But again, I can't imagine what you expected it to do.
3) In this last one:
h = plot(magic(3));
set(h,{'DisplayName'},{'first';'second';'third'})
legend show
hold on
plot(h,magic(3))
set(h,{'DisplayName'}, ...
{'first';'second';'third';'first';'second';'third'})
You're getting an error which says:
Error using matlab.graphics.chart.primitive.Line/set
Value cell array handle dimension must match handle vector length.
That's because your array h has 3 elements, but you've given it 6 values for the DisplayName property. As it's saying, the "cell array handle dimension" must match the "handle vector length".
My guess is that what you're trying to do is something like this:
h = gobjects(1,6);
h(1:3) = plot(randn(3));
hold on
h(4:6) = plot(randn(3));
set(h,{'DisplayName'}, ...
{'A';'B';'C';'D';'E';'F'})
1/ Indeed I misunderstood. I thought the innovation was the brackets { } around DisplayName. But also using "set" was important. I see that now.
2/ I attempted to use "plot(ax,x,y)" ... but clearly in a bad way.
3/ Yes this is about what I am atempting to achieve. The only issue remaining is that the following code does not overwrite/update the DisplayNames ...
h(1:3) = plot(randn(3));
set(h,{'DisplayName'}, ...
{'A';'B';'C'})
legend show
pause(5)
hold on
h(4:6) = plot(randn(3));
set(h,{'DisplayName'}, ...
{'A';'B';'C';'D';'E';'F'})
legend show
It only updates when switching the legends off and on again.
legend off
legend show
Thank you for the help
Hey Mike,
How do I set the interpreter for the 'DisplayName' strings as latex ?
Thanks in advance!
Hi, i think this will work for you..it work for me.
plot(gammadB,PcovM,':+b','DisplayName','coverage','linewidth',2,'markersize',9);
h1= legend('location', 'best');
set(h1,'Interpreter','latex');
set(h1,'FontSize',32);

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Programming dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by