Apply changes to ALL UIAxes in AppDesigner?

I need to make an app in Appdesigner which will contain 19 axes. I want to make changes to the axes according to:
function startupFcn(app)
yyaxis(app.UIAxes2,'right')
app.UIAxes2.YLabel.String='NAME for your RIGHT Y Axis';
app.UIAxes2.YLim = [0 25];
app.UIAxes2.YColor = [1 0 0]; % font colour
yyaxis(app.UIAxes2,'left')
app.UIAxes2.YLabel.String='NAME for your LEFT Y Axis';
app.UIAxes2.YLim = [0 100];
app.UIAxes2.Color = [1 1 0.8]; % plot box colour
app.UIAxes2.XGrid = 'on';
app.UIAxes2.YGrid = 'on';
end
But since the axes are named "UIAxes", "UIAxes2", "UIAxes3" and so on, I need to write the code above several times for each UIAxes, which is tedious work. Is there any way to run a for command that goes through all the axes? Just using "for" and then strcat() does not seem to work.
Thank you for your help!

Answers (1)

Adam Danz
Adam Danz on 12 Mar 2019
Edited: Adam Danz on 25 Mar 2019
Option 1: optimize one of your axes and then copy it.
If you haven't created the 19 axes in your app yet, create the first one, perfect it, and then just copy/paste it as many times as you need. Some modifications (such as the right side y axis) can't be made in design mode and you'll have to use option 2 for those modifications but the more changes you can make at this level, the better.
Option 2: write a function and send all axis handles into the function.
Start a new function in your app where the first input is app (required) and the 2nd input is 'axisHandle'. Move all of your parameter adjustments to that function and apply them all to "axisHandle". Then in your startup fcn, you'll just send each axis handle into the function.
function startupFcn(app)
fixAxes(app, app.UIAxes)
fixAxes(app, app.UIAxes2)
fixAxes(app, app.UIAxes3)
fixAxes(app, app.UIAxes4)
end
function fixAxes(app, axisHandle)
%ignore erroneous warnings to specify axis handles as first argument
yyaxis(axisHandle,'right')
axisHandle.YLabel.String='NAME for your RIGHT Y Axis';
axisHandle.YLim = [0 25];
axisHandle.YColor = [1 0 0]; % font colour
yyaxis(axisHandle,'left')
axisHandle.YLabel.String='NAME for your LEFT Y Axis';
axisHandle.YLim = [0 100];
axisHandle.Color = [1 1 0.8]; % plot box colour
axisHandle.XGrid = 'on';
axisHandle.YGrid = 'on';
end
Not tested
Option 2b: use a loop in option 2
function startupFcn(app)
allAxes = [app.UIAxes, app.UIAxes2, app.UIAxes3, app.UIAxes4];
for i = 1:length(allAxes)
fixAxes(app, allAxes(i))
end
end
% or the loop could be within the fixAxes() function and you could send in 'allAxes'.

4 Comments

Thank you for your answer, I believe the problem is solved soon. I still have a problem though, the code I am using at the moment is shown at the bottom. App designer has a problem with the array "allAxes" that I want to contain all the UIAxes. It has a problem with the very first comma after "app.UIAxes" and says the following:
"Error using app1
Error: File: app1.mlapp Line: 32 Column: 284
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters."
Why is this? Can I not create an array with all the axes and refer to them as I have?
methods (Access = private)
function startupFcn(app)
allAxes = (app.UIAxes , app.UIAxes2, app.UIAxes3, app.UIAxes4, app.UIAxes5, app.UIAxes6, app.UIAxes7, app.UIAxes8, app.UIAxes9, app.UIAxes10, app.UIAxes11, app.UIAxes12, app.UIAxes13, app.UIAxes14, app.UIAxes15, app.UIAxes16, app.UIAxes17, app.UIAxes18, app.UIAxes19);
for i = 1:length(allAxes)
fixAxes(app, allAxes(i))
end
end
function fixAxes(app, axisHandle)
%ignore erroneous warnings to specify axis handles as first argument
yyaxis(axisHandle,'right')
axisHandle.YLabel.String='Global force [N]';
%axisHandle.YLim = [0 25];
axisHandle.YColor = [1 0 0]; % font colour
yyaxis(axisHandle,'left')
axisHandle.YLabel.String='Biotac Impedance [k \Omega]';
axisHandle.YLim = [0 22];
axisHandle.Color = [1 1 0.8]; % plot box colour
axisHandle.XGrid = 'on';
axisHandle.YGrid = 'on';
end
end
Oops. Replace the parentheses with square brackets (I corrected that line in my solution).
allAxes = [app.UIAxes , app.UIAxes2, app.UIAxes19];
Thanks!!
By the way. The resulting figure containing all 19 plots is a lot bigger than my screen. I would like to both pan and zoom in the figure. Is this possible? I have been trying to find a way to add a scroll function to the figure but failed so far. Do you know how to fix this? It would be nice to be able to zoom into the figure to a particular plot, and then also be able to furhter zoom into that plot. I hope I am making some sense.
You can pan/zoom through axes but that's not possible to do on a figure as far as I know. The better solution would be to set the figure size to your monitor size.
figure('units','normalized','outerposition',[0 0 1 1])
But with 19 subplot, that will probably reduce the size of the subplot axes so much that their utility would suffer. It would be nice for matlab to build a scroll bar on figures, though.
I suggest breaking your plots up into multiple figures.

Sign in to comment.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Asked:

on 12 Mar 2019

Edited:

on 12 Dec 2019

Community Treasure Hunt

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

Start Hunting!