MATLAB Answers

How to Plot on UI Axes with app designer?

19 views (last 30 days)
Cameron Schuetz
Cameron Schuetz on 10 Nov 2020
Commented: Geoff Hayes on 11 Nov 2020
I am trying to have a graph ploted on the UI Axes in my GUI that does not open a new figure but instead just changes the graph that is being shown.
I would like to graph just individual points on said graph and be able to change the axis of the graph through edit windows and use a "plot" button to have the points added.
My current code looks like this:
function PlotButtonPushed(app, event)
vecx = str2double(strsplit(app.XValuesEditField.Value));
vecy = str2double(strsplit(app.YValuesEditField.Value));
Lx_val = length(vecx);
Ly_val = length(vecy);
min_X = app.XminEditField.Value;
max_X = app.XmaxEditField.Value;
scale = (max_X-min_X)/5;
max_Y = app.YmaxEditField.Value;
min_Y = app.YminEditField.Value;
scale1 = (max_Y-min_Y)/5;
selectedButton(1) = app.Color.SelectedObject;
if selectedButton(1) == app.Red
C = 2;
elseif selectedButton(1) == app.Green
C = 3;
else
C = 1;
end
selectedButton(2) = app.Shape.SelectedObject;
if selectedButton(2) == app.Dashed
S = 2;
elseif selectedButton(2) == app.Solid
S = 3;
else
S =1;
end
if C==1 && S==1
if Lx_val == Ly_val
i = 1;
while i <= Lx_val
hold on;
plot(app.UIAxes,vecx(i),vecy(i),'ko');
app.UIAxes.YTickLabel = [min_Y:scale1:max_Y];
app.UIAxes.XTickLabel = [min_X:scale:max_X];
xlim(app.UIAxes,[min_X max_X]);
ylim(app.UIAxes,[min_Y max_Y]);
i = i+1;
end
end
end
When I run the file I can edit the axis of the graph initially without issue, however once I hit plot and it runs this piece of code here, it opens an empty plot figure, plots the final point that I ask it to, and squishes my x and y axis into the bottom left corner like the image I have attached. (If the way the code is in the body is sloppy I can always take a screenshot of that as well!). If anybody could help I would really appreciate it.

Answers (1)

Geoff Hayes
Geoff Hayes on 10 Nov 2020
Cameron - from hold, try replacing your line of code
hold on;
with
hold(app.UIAxes, 'on');
where you specify which axes you want to apply the hold to.
  2 Comments
Geoff Hayes
Geoff Hayes on 11 Nov 2020
Cameron - I suspect what is happening is that your data contains values outside of the limits set by the axes, and (for whatever reason) those limits aren't being respected. Try setting the limits after the plots have been drawn to the axes
hold(app.UIAxes, 'on'); % this may work outside of the loop too but please check
while i <= Lx_val
plot(app.UIAxes,vecx(i),vecy(i),'ko');
i = i+1;
end
% now set the limits
app.UIAxes.YTickLabel = [min_Y:scale1:max_Y];
app.UIAxes.XTickLabel = [min_X:scale:max_X];
xlim(app.UIAxes,[min_X max_X]);
ylim(app.UIAxes,[min_Y max_Y]);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!