Problem with text in legend of figure file generated through plot command

When i get a figure file by using plot command everything is right but when I try to replace the legend default text to what I want to label it, the text which I key in through my keyboard is changed to some other text. I closed and open the matlab again but still same problem.

2 Comments

Can you include some details about your code and output.
Why not specify the legend labels programmatically in your call to legend() or by using the DisplayName property of plot objects?

Answers (1)

Here's an example:
clc
close all
clear all
% fake data
f = -pi:0.01:pi;
y1 = sin(2.*f + pi/3) + 0.05*rand(size(f));
y2 = sin(3.*f + pi/2) + 0.05*rand(size(f));
y3 = sin(2.5.*f + 0) + 0.05*rand(size(f));
y4 = sin(0.75.*f + pi/6) + 0.05*rand(size(f));
% code
figure;
l1 = plot(f, y1, 'r','DisplayName','My Data 1');
hold on
l2 = plot(f, y2, 'color', [0.8 0.7 0],'DisplayName','My Data 2');
l3 = plot(f, y3, 'b','DisplayName','My Data 3');
l4 = plot(f, y4, 'color', [0 0.75 0],'DisplayName','My Data 4');
hleg = legend('location','best');
hold off
xlabel('xdata [radians]');
ylabel('ydata');
ylim([-1.2 1.2]);
xlim([-pi pi]);
title('just some random data');
% rename the data, change text properties
hleg.String = {'Random 1','Random 2','Random 3','Random 4'};
hleg.FontName = 'Ariel';
hleg.FontSize = 12;
hleg.FontWeight = 'bold';

This question is closed.

Products

Release

R2015a

Tags

Asked:

on 6 Jan 2020

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!