gca method to adjust axes properties not working within function
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Tom Chernowsky
am 9 Jan. 2024
Kommentiert: Tom Chernowsky
am 9 Jan. 2024
Hello,
I have a function which adjusts some properties of figures after they have been plotted. The function takes the axes graphical object as argument. It goes like this:
function MakeItLookPretty(ax)
ax.YAxis.FontName='Open Sans Bold'
ax.YAxis.FontSize=13
ax.XAxis.TickLength=[0 0]
% and so on...
end
I used to be able to call the function:
axes = gca
MakeItLookPretty(axes)
and it adjusted properties of axes of the current figure. This, however, stopped working since I updated to MATLAB2023b. Calling the function does not throw an error, it seemingly does nothing. Interestingly, if I run the block of code "standalone" (not inside a function) it adjusts the axes perfectly.
What am I missing?
2 Kommentare
Stephen23
am 9 Jan. 2024
Do not name your variables "axes".
It works on this forum:
matlabRelease
plot(rand(5,2))
MakeItLookPretty(gca)
function MakeItLookPretty(ax)
ax.YAxis.FontName='Open Sans Bold';
ax.YAxis.FontSize=13;
ax.XAxis.TickLength=[0,0];
% and so on...
end
Akzeptierte Antwort
Hassaan
am 9 Jan. 2024
% Call the sample plotting function
createSamplePlot();
% Sample plotting function
function createSamplePlot()
% Create some sample data
x = linspace(0, 2*pi, 100);
y = sin(x);
% Plot the data
plot(x, y);
title('Sample Plot');
% Get the current axes handle
ax = gca;
% Call the custom function to make the plot look pretty
MakeItLookPretty(ax);
end
% Custom function to modify the appearance of the plot
function MakeItLookPretty(ax)
% Modify various properties of the axes
ax.YAxis.FontName = 'Open Sans Bold';
ax.YAxis.FontSize = 13;
ax.XAxis.TickLength = [0 0];
% Add more customization as needed...
end
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!