How to Plot on UI Axes with app designer?

조회 수: 259 (최근 30일)
Cameron Schuetz
Cameron Schuetz 2020년 11월 10일
댓글: Geoff Hayes 2020년 11월 11일
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.

답변 (1개)

Geoff Hayes
Geoff Hayes 2020년 11월 10일
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
Cameron Schuetz
Cameron Schuetz 2020년 11월 11일
This worked to plot the graph on the UIAxes without creating a new figure,
however I still run into the issue of the labeling of the axis being squished into the origin (see attached image)
Geoff Hayes
Geoff Hayes 2020년 11월 11일
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]);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by