How to get legend entries from existing figure ?

조회 수: 35 (최근 30일)
Petr Michalek
Petr Michalek 2025년 12월 9일 13:28
댓글: Petr Michalek 2025년 12월 10일 8:33
Hello,
I am extracting the data from existing figures, this code works for the x, y data, but the Legend variable does not contain any values.
My code:
clear; close all;
addpath(genpath('D:\Uprofily\Blockage\numerika\figures'));
fig1 = openfig("cl_mean.fig");
Legend = findobj(gcf,'Type','Legend')
% data extract
dataObjs1x = findobj(fig1,'-property','Xdata'); % data X
xdata = dataObjs1x(1).XData;
dataObjs2y = findobj(fig1,'-property','Ydata'); % data Y
y1 = dataObjs2y(1).YData; % line y1
y2 = dataObjs2y(2).YData; % line y2
save("cl_mean.mat");
How do I get the legend entries, i.e. the "rect2 Comsol 2d, v=2.8, greatdomain" and some designation which entry belongs to which line ?
Thank you. Petr

채택된 답변

Matt J
Matt J 2025년 12월 9일 13:39
편집: Matt J 2025년 12월 9일 13:53
The legend's String property will get you the legend labels for the different lines, e.g.,
plot(rand(5,3)); L=legend;
L.String
ans = 1×3 cell array
{'data1'} {'data2'} {'data3'}
EDIT: Handles to the original line data can be recovered from,
Hlines = L.PlotChildren
Hlines =
3×1 Line array: Line (data1) Line (data2) Line (data3)
  댓글 수: 3
Matt J
Matt J 2025년 12월 9일 18:15
편집: Matt J 2025년 12월 9일 18:15
Use the syntax,
openfig(___,visibility)
Petr Michalek
Petr Michalek 2025년 12월 10일 8:33
When I load only the variables xdata, y1, y2, String and PlotChildren, the figure fig1 does not open, so this is also a solution.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by