gca method to adjust axes properties not working within function

3 views (last 30 days)
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
This statement is not inside any function.
(It follows the END that terminates the definition of the function "MakeItLookPretty".)
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 Comments
Stephen23
Stephen23 on 9 Jan 2024
Do not name your variables "axes".
It works on this forum:
matlabRelease
ans =
matlabRelease with properties: Release: "R2023b" Stage: "release" Update: 5 Date: 01-Dec-2023
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
Tom Chernowsky
Tom Chernowsky on 9 Jan 2024
Yep, it works, the problem was something else entirely. Thank you & sorry to have wasted your time.

Sign in to comment.

Accepted Answer

Hassaan
Hassaan on 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.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!