How to use a user plotting function within an app
5 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Adam Danz
on 8 Jun 2023
Edited: Adam Danz
on 8 Jun 2023
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 Comments
More Answers (0)
See Also
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!