How to use a user plotting function within an app

9 ビュー (過去 30 日間)
ED
ED 2023 年 6 月 8 日
コメント済み: ED 2023 年 6 月 8 日
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

採用された回答

Adam Danz
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 件のコメント
Adam Danz
Adam Danz 2023 年 6 月 8 日
answer updated
ED
ED 2023 年 6 月 8 日
Exactly what I needed, thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by