Use of nested function in MATLAB app designer

I want to access data and use for mutiple push buttons to plot graphs. For ImportdataButtonPushed import the data then for torqueButtonPushed I want to access and use data which imported in Import push button.
% Button pushed function: torqueButton
function torqueButtonPushed(app, event)
plot(app.UIAxes,[A.Speed_1_Total_WT1],[A.Torque_1_Total_WT1])
plot(app.UIAxes,[A.P_SigmaA_Total_WT1],[A.Eta1_1_Total_WT1])
However when I tried to plot graph it gives me error and I unable to plot subplot. Otherwise I have to add ten push buttons for ten plots. Using nested function how can use data to plot graph?
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Button PrivateButtonPushedFcn.

12 Comments

What is the complete error message?
It looks to me like you should store the loaded data in a private property. The you can simply use app.A.Torque_1_Total_WT1 to load the data. (I would also recommend changing the name of that variable; it looks like you're storing data in variable names, instead of in variables. Use index arrays instead: app.A.Torque{1}.Total_WT{1})
AppDesigner apps are based on uifigure. That means you will have to use explicit handles in most cases, and some functions will not work at all, as they have to be implemented separately.
I am still trying use subplots but not able to solve and Even I cannot change title for various plots?
Now you have even deleted the little code you did post. Are you using explicit handles yet?
Yes I resolved the double y axis issue. So I deleted. Now I getting problem with changing x and y axis label. Every new plot I have to modify it. Any alternative solution available?
Please post your code.
I posted new code .Now I getting problem with changing x and y axis label. Every new plot I have to modify it. Any alternative solution available?
classdef app1import < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
InducedvoltagevsspeedButton matlab.ui.control.Button
AvgPowervsEffButton matlab.ui.control.Button
SpeedvsTorquevsPowerButton matlab.ui.control.Button
torquevsSpeedButton matlab.ui.control.Button
ImportdataButton matlab.ui.control.Button
UIAxes4 matlab.ui.control.UIAxes
UIAxes3 matlab.ui.control.UIAxes
UIAxes2 matlab.ui.control.UIAxes
UIAxes matlab.ui.control.UIAxes
end
properties (Access = public)
Property
A % Description
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: ImportdataButton
function ImportdataButtonPushed(app, event)
[file,path,~]=uigetfile('*.csv');
t=[path file];
app.A = readtable(t);
app.A.P_SigmaA_Total_WT1=(app.A.P_SigmaA_Total_WT1)./1000;
app.A.Time=time2num(app.A.Time,"minutes");
app.A.U_SigmaA_Total_WT1=(app.A.U_SigmaA_Total_WT1)./(1.732);
app.A.Eta1_1_Total_WT1=(app.A.Eta1_1_Total_WT1).*(1);
app.A.Speed_1_Total_WT1(ismissing(app.A.Speed_1_Total_WT1)) = 0;
app.A.Torque_1_Total_WT1(ismissing(app.A.Torque_1_Total_WT1)) = 0;
app.A.U_SigmaA_Total_WT1(ismissing(app.A.U_SigmaA_Total_WT1)) = 0;
%Efficiency range 0 to 100% variable table11
app.A = app.A(app.A.Eta1_1_Total_WT1 >= 0 & app.A.Eta1_1_Total_WT1 <= 100,:);
end
% Button pushed function: torquevsSpeedButton
function torquevsSpeedButtonPushed(app, event)
plot(app.UIAxes,[ app.A.Torque_1_Total_WT1],[ app.A.Speed_1_Total_WT1])
end
% Button pushed function: SpeedvsTorquevsPowerButton
function SpeedvsTorquevsPowerButtonPushed(app, event)
yyaxis(app.UIAxes2,'left')
plot(app.UIAxes2,[app.A.Speed_1_Total_WT1],[app.A.Torque_1_Total_WT1]);
yyaxis(app.UIAxes2,'right')
plot(app.UIAxes2,[app.A.Speed_1_Total_WT1],[app.A.P_SigmaA_Total_WT1]);
end
% Callback function
function TorqueEditFieldValueChanged(app, event)
value = app.TorqueEditField.Value;
plot(app.UIAxes3,[app.A.P_SigmaA_Total_WT1],[app.A.Eta1_1_Total_WT1])
end
% Button down function: UIAxes
function UIAxesButtonDown(app, event)
end
% Button pushed function: AvgPowervsEffButton
function AvgPowervsEffButtonPushed(app, event)
plot(app.UIAxes3,[app.A.P_SigmaA_Total_WT1],[app.A.Eta1_1_Total_WT1 ])
end
% Button pushed function: InducedvoltagevsspeedButton
function InducedvoltagevsspeedButtonPushed(app, event)
plot(app.UIAxes4,[app.A.Speed_1_Total_WT1],[app.A.U_SigmaA_Total_WT1 ])
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 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'Torque')
ylabel(app.UIAxes, 'Speed')
zlabel(app.UIAxes, 'Z')
app.UIAxes.ButtonDownFcn = createCallbackFcn(app, @UIAxesButtonDown, true);
app.UIAxes.Position = [300 296 300 185];
% Create UIAxes2
app.UIAxes2 = uiaxes(app.UIFigure);
title(app.UIAxes2, 'Speed vs Torque vs Power')
xlabel(app.UIAxes2, 'Speed (rpm)')
ylabel(app.UIAxes2, 'Torque(Nm)')
zlabel(app.UIAxes2, 'Z')
app.UIAxes2.Position = [1 296 300 185];
% Create UIAxes3
app.UIAxes3 = uiaxes(app.UIFigure);
title(app.UIAxes3, 'Power vs Efficiency')
xlabel(app.UIAxes3, 'Power (kW)')
ylabel(app.UIAxes3, 'Efficiency(%)')
zlabel(app.UIAxes3, 'Z')
app.UIAxes3.Position = [1 64 300 185];
% Create UIAxes4
app.UIAxes4 = uiaxes(app.UIFigure);
title(app.UIAxes4, 'Speed vs Induced voltage')
xlabel(app.UIAxes4, 'Speed (RPM)')
ylabel(app.UIAxes4, 'Induced voltage (V)')
zlabel(app.UIAxes4, 'Z')
app.UIAxes4.Position = [300 64 300 185];
% Create ImportdataButton
app.ImportdataButton = uibutton(app.UIFigure, 'push');
app.ImportdataButton.ButtonPushedFcn = createCallbackFcn(app, @ImportdataButtonPushed, true);
app.ImportdataButton.Position = [267 267 100 22];
app.ImportdataButton.Text = 'Import data';
% Create torquevsSpeedButton
app.torquevsSpeedButton = uibutton(app.UIFigure, 'push');
app.torquevsSpeedButton.ButtonPushedFcn = createCallbackFcn(app, @torquevsSpeedButtonPushed, true);
app.torquevsSpeedButton.Position = [411 267 103 22];
app.torquevsSpeedButton.Text = 'torque vs Speed';
% Create SpeedvsTorquevsPowerButton
app.SpeedvsTorquevsPowerButton = uibutton(app.UIFigure, 'push');
app.SpeedvsTorquevsPowerButton.ButtonPushedFcn = createCallbackFcn(app, @SpeedvsTorquevsPowerButtonPushed, true);
app.SpeedvsTorquevsPowerButton.Position = [72 267 158 22];
app.SpeedvsTorquevsPowerButton.Text = 'Speed vs Torque vs Power';
% Create AvgPowervsEffButton
app.AvgPowervsEffButton = uibutton(app.UIFigure, 'push');
app.AvgPowervsEffButton.ButtonPushedFcn = createCallbackFcn(app, @AvgPowervsEffButtonPushed, true);
app.AvgPowervsEffButton.Position = [81 31 106 22];
app.AvgPowervsEffButton.Text = 'Avg Power vs Eff';
% Create InducedvoltagevsspeedButton
app.InducedvoltagevsspeedButton = uibutton(app.UIFigure, 'push');
app.InducedvoltagevsspeedButton.ButtonPushedFcn = createCallbackFcn(app, @InducedvoltagevsspeedButtonPushed, true);
app.InducedvoltagevsspeedButton.Position = [400 31 152 22];
app.InducedvoltagevsspeedButton.Text = 'Induced voltage vs speed';
% 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 = app1import
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
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
Your question is not clear to me. You seem to be well aware how to change the axis labels, as you do so multiple times in this code. What is your specific issue?
Hello for every new graph I have to add new UI axes with new lables which is time consuming. Instead of creating UI axis for every new plot with every new labels , how can solve issue of axis labels and UI axis plot in single
What do you want? Multiple axes with one graph each? Or one axis that shows one graph at a time? You seem to have implemented the first option. The second option seems easy enough to implement: move all the label setting to the callback functions.
I want to plot 10 graphs with two axes. One axis that shows one graph at a time Like x1 vs y1, x2 vs y2 ..... x10 vs y10. It is fine I can type name of axis label.
That doesn't answer my question.
What do you want? 10 axes, or 1? The total in either case would be 10 graphs.
And what part do you want to automate? You can write a small function that will create a uiaxes object and a button, but you will still have to type the text itself. Is that what you would want?
Try to make it easy for me to help you. It is easy for me to ignore this thread if it is more frustrating than rewarding. I doubt that is what you want.

Sign in to comment.

 Accepted Answer

You can use the callback functions of your buttons to replace the graphs and labels:
function torquevsSpeedButtonPushed(app, event)
plot(app.UIAxes,app.A.Torque_1_Total_WT1,app.A.Speed_1_Total_WT1)
title(app.UIAxes, 'Speed vs Torque vs Power')
xlabel(app.UIAxes, 'Speed (rpm)')
ylabel(app.UIAxes, 'Torque(Nm)')
zlabel(app.UIAxes, 'Z')
end

4 Comments

Thank you for solution. Just want to know can't we use subplot instead for x1 vs y1,x2 vs y2...x10 vs y10
subplot just creates extra axes objects and takes care of the spacing for you. There is no reason why you can't create multiple uiaxes to get a similar effect, but you wrote you didn't want multiple axes.
Just wondering how can plot histogram in matlab app designer?
You can either use histcounts and use bar, or you can use histogram. As with any GUI: make sure you specify your axes (or in your case uiaxes) as the parent object.

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 21 Jun 2021

Commented:

Rik
on 1 Jul 2021

Community Treasure Hunt

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

Start Hunting!