How can I insert a variable into a plot legend, with a special character following the variable?

11 visualizzazioni (ultimi 30 giorni)
I am attempting to create a specific legend on a plot. I am intentionally ignoring the first 3 sets of data, because I don't want them to be labeled in the legend. I only want the legend to display the fourth data set and read: 'Gradient: [insert variable value] °/mm'. In my case, the varibale value I am trying to display is 'M'. I have tried a bunch of things, but have not yet been successful in displaying both the variable value and the degree symbol for the units following the variable. The following is the code I am currently trying, but is unsuccessful:
m=(y2-y1)/(x2-x1);
M=abs(m);
str=num2str(M);
legend('','','','Gradient:',eval(str),'\circ/mm')
Any help would much appreciated. Thank you.

Risposta accettata

Stephen23
Stephen23 il 7 Ott 2024
Modificato: Stephen23 il 7 Ott 2024
Using STRING:
plot(rand(5,4))
m = pi;
legend('','','',"Gradient: "+m+"\circ/mm")
Using CHAR:
plot(rand(5,4))
m = pi;
t = sprintf('Gradient: %.5g \\circ/mm',m);
legend('','','',t)

Più risposte (5)

Les Beckham
Les Beckham il 7 Ott 2024
plot(magic(4))
M = 10.5; %<<< made up value for variable M
legend('', '', '', sprintf('Gradient: %.1f\\circ/mm', M))

Mitchell Thurston
Mitchell Thurston il 7 Ott 2024
You can use sprintf formatting to get the variable inserted. You can also set the legend interpreter for latex formatting to get the degree symbol formatted.
plot(0:360, sind(0:360)+[1;2;3;4])
var = exp(1);
legend('','','',sprintf('Gradient: $%.2f^\\circ$/mm', var),'interpreter','latex')
Also, if you don't want to do the empty strings on the left, you can use the option flag plot(x,y,"handlevisibility","off") on the first 3 plots.

Shivam Gothi
Shivam Gothi il 7 Ott 2024
In order to resolve the issue, I have defined two vectors "x" and "y" and ploted them using "plot()" function. Then I added a legend which follows the format as explained by you in the question.
%create x and y vectors
x=1:1:10;
y=1:1:10;
%arbitrarily define "x2", "x1","y1" and "y2"
y2=7;
y1=2;
x2=10;
x1=5;
%% your code
m=(y2-y1)/(x2-x1);
M=abs(m);
str=num2str(M);
% create a string to print as legend
legend_string = "Gradient: " +str+'\circ/mm' %using string concatenation property
legend_string = "Gradient: 1\circ/mm"
%create plot
plot(x,y);
legend(legend_string);
I hope this is as per your expectations !

Zinea
Zinea il 7 Ott 2024
I can see that you want the legend should display only the fourth data set and insert ‘°/mm’ following the variable. To achieve this, instead of attempting to concatenate strings and variables directly with the ‘legend’ function, the string should be formatted beforehand and then passed to the ‘legend’ function as given in the code snippet below:
legendText = sprintf('Gradient: %.2f \\circ/mm', M);
Output figure using code with above modification and dummy variables :
Hope it helps, cheers!

Steven Lord
Steven Lord il 7 Ott 2024
In addition to what the others have said, rather than passing empty strings into the legend function to skip one or more of the lines, I recommend instead passing the handles of the lines that you want to see in the legend into the function. If you do the legend will only include those lines.
Let's set up some data.
x = 0:360;
y1 = sind(x);
y2 = cosd(x);
rangeDescription = "on the range [" + min(x) + ", " + max(x) + "]\circ";
Now I'll create two plots with legends. In the first I'll call legend with just one of the handles and the corresponding label. The axis call and the hold let me control the appearance of the axes.
axis([0 360 -1 1])
hold on
h(1) = plot(x, y1, '-');
h(2) = plot(x, y2, ':');
legend(h(2), "cosine " + rangeDescription) % one handle + one label
Alternately, when you plot the data you can specify the DisplayName property to set the string that will be displayed in the legend. If you do this, you can call legend with just the handle(s). Unlike the previous plot, this time I included the plot of the sine function in the legend.
figure
axis([0 360 -1 1])
hold on
k(1) = plot(x, y1, '-', 'DisplayName', "sine " + rangeDescription); % label set here
k(2) = plot(x, y2, ':', 'DisplayName', "cosine " + rangeDescription); % and here
legend(k(1)) % just pass in the handle(s) that you want the legend to include

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by