How to use a user plotting function within an app
조회 수: 6 (최근 30일)
이전 댓글 표시
I want to use custom functions to generate plots from within a matlab app. I want to pass data from the app to the function, then I want the function to plot data onto the UIfigure. The problem is when testfunc runs, it plots data in a new figure. I need to use a custom plotting function due to the complexity of the code I plan to use, so I prefer to keep that outside of the app.
Here's an example of what I have tried:
classdef Comp_Mapp < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
UIAxes2 matlab.ui.control.UIAxes
UIAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
x = [0:10:1000];
y1 = x*1;
y2 = x*2;
plot(app.UIAxes,x,y1)
hold on
plot(app.UIAxes,x,y2)
hold off
axes(app.UIAxes2);
testfunc(x,y1)
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 1011 636];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'Time')
ylabel(app.UIAxes, 'Speed/Torque')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [1 377 450 260];
% Create UIAxes2
app.UIAxes2 = uiaxes(app.UIFigure);
title(app.UIAxes2, 'Title')
xlabel(app.UIAxes2, 'X')
ylabel(app.UIAxes2, 'Y')
zlabel(app.UIAxes2, 'Z')
app.UIAxes2.Position = [441 7 570 630];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = Comp_Map1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
testfunc:
function [] = testfunc(x,y)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
plot(x,y)
end
댓글 수: 0
채택된 답변
Adam Danz
2023년 6월 8일
편집: Adam Danz
2023년 6월 8일
Specify the axes handle,
testfunc(app.UIAxes,x,y1)
function [] = testfunc(ax,x,y)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
plot(ax,x,y)
end
If you cannot modify the function and it does not have an input argument that accepts an axes handle or app handle, then you'll have to turn the handle visibility of the app on and make the axes current.
I've explained those steps here
댓글 수: 3
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File 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!