Effacer les filtres
Effacer les filtres

Change active elements of Plot Browser from code

5 vues (au cours des 30 derniers jours)
Julian Braun
Julian Braun le 22 Mar 2023
Commenté : Julian le 3 Août 2023
Hey, I would like to change the active elements of an existing figure with code.
Currently I have to open the Plot Browser and change every element manuelly.
Is there a way to change them with code?

Réponse acceptée

Yash
Yash le 28 Mar 2023
Hi Julian Braun,
Yes, you can change the active elements of a figure programmatically using MATLAB code. The following code demonstrates how to change the properties of the X-axis and Y-axis labels, the title, and the legend of a figure:
% Create some sample data and plot it
x = 1:10;
y = sin(x);
% Create a figure to visualize your data
% The variable myFig is a handle to your figure.
myFig = figure;
% Plot your data;
p = plot(x, y)
% Get the axis of your plot
ax = p.Parent;
% With the handle to the axis you can change the active elements
% Change the X-axis label
ax.XLabel.String = 'Time (s)'
% Change the Y-axis label
ax.YLabel.String = 'Amplitude'
% Change the title
ax.Title.String = 'Sine Wave'
You can alternatively set the active elements by the below code as well
% Change the X-axis label
xlabel('Time (s)')
% Change the Y-axis label
ylabel('Amplitude')
% Change the title
title('Sine Wave')
% Change the legend
legend('Sine')
You can also access and modify these properties directly using the gca (get current axes) and gcf (get current figure) functions, which return handles to the current axes and figure, respectively. For example, you can change the font size and font weight of the X-axis label like this:
ax = gca;
ax.XLabel.FontSize = 16;
ax.XLabel.FontWeight = 'bold';
You can similarly modify other properties of the axes, such as the tick labels and tick marks, using the XTick, XTickLabel, YTick, and YTickLabel properties of the axes. For more information on customizing plots in MATLAB, see the documentation on graphics customization:
I hope this helps :)
  1 commentaire
Julian
Julian le 3 Août 2023
Thanks for your helpful explanation!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Labels and Annotations dans Help Center et File Exchange

Tags

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by