MATLAB Answers

How read CSV data on MATLAB APP?

23 views (last 30 days)
I am facing problem with reading csv file data and plottng graph of torque vs speed and other plots on MATLAB app which I developed. How can I solve plotting graph and data reading issue
[file,path,~]=uigetfile('*.csv');
t=fullfile(path,file);
app.A = readtable(t);
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)
% Code that executes after component creation
function startupFcn(app)
yyaxis(app.UIAxes,'right')
app.UIAxes.YLabel.String='Power (kW)';
% app.UIAxes.YLim = [0 25];
app.UIAxes.YColor = [1 0 0]; % font colour
yyaxis(app.UIAxes,'left')
app.UIAxes.YLabel.String='Torque(Nm)';
% app.UIAxes.YLim = [0 100];
app.UIAxes.Color = [1 1 0.8]; % plot box colour
app.UIAxes.XLabel.String='Speed(RPM)'
app.UIAxes.XGrid = 'on';
app.UIAxes.YGrid = 'on';
%axes 3
end
% Button pushed function: ImportdataButton
function ImportdataButtonPushed(app, event)
[file,path,~]=uigetfile('*.csv');
t=fullfile(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,:);
app.A.Eta1_1_Total_WT1(ismissing(app.A.Eta1_1_Total_WT1)) = 0;
app.A.P_SigmaA_Total_WT1(ismissing(app.A.P_SigmaA_Total_WT1)) = 0;
end
% Button pushed function: torquevsSpeedButton
function torquevsSpeedButtonPushed(app, event)
plot(app.UIAxes2,app.A.Torque_1_Total_WT1,app.A.Speed_1_Total_WT1)
title(app.UIAxes2, 'Speed vs Torque')
xlabel(app.UIAxes2, 'Speed (rpm)')
ylabel(app.UIAxes2, 'Torque(Nm)')
zlabel(app.UIAxes2, 'Z')
end
% Button pushed function: SpeedvsTorquevsPowerButton
function SpeedvsTorquevsPowerButtonPushed(app, event)
yyaxis(app.UIAxes,'left')
plot(app.UIAxes,[app.A.Speed_1_Total_WT1],[app.A.Torque_1_Total_WT1]);
yyaxis(app.UIAxes,'right')
plot(app.UIAxes,[app.A.Speed_1_Total_WT1],[app.A.P_SigmaA_Total_WT1]);
title(app.UIAxes, 'Speed vs Torque vs Power')
end
% Callback function
function TorqueEditFieldValueChanged(app, event)
end
% Button down function: UIAxes
function UIAxesButtonDown(app, event)
end
% Button pushed function: AvgPowervsEffButton
function AvgPowervsEffButtonPushed(app, event)
% Set the Table size for Motor and Temperature csv file
%TimenewT=app.Temp(app.Temp.Time1 >= 500 & app.Temp.Time1 <= 700,:);
%TimenewM = app.A(app.A.Time >= 500 & app.A.Time <=700,:);
%TimenewM = TimenewM(TimenewM.Speed_1_Total_WT1 <= 4200,:); % Set Motor Speed
%xy=TimenewM(1:250,:); %motor performance csv table
%xy1=TimenewT(1:i3,:);
%plot(app.UIAxes2,[xy.Time],[xy.Speed_1_Total_WT1]);
plot(app.UIAxes3,[app.A.P_SigmaA_Total_WT1],[app.A.Eta1_1_Total_WT1]);
title(app.UIAxes3, 'Power(kW) vs Efficiency(%)')
xlabel(app.UIAxes3, 'Power(kW)')
ylabel(app.UIAxes3, 'Efficiency(%)')
end
% Button pushed function: InducedvoltagevsspeedButton
function InducedvoltagevsspeedButtonPushed(app, event)
plot(app.UIAxes4,[app.A.Speed_1_Total_WT1],[app.A.U_SigmaA_Total_WT1 ])
title(app.UIAxes4, 'Speed(RPM) vs Induced Voltage(V)')
xlabel(app.UIAxes4, 'Speed (RPM)')
ylabel(app.UIAxes4, 'Induced voltage(V)')
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.XGrid = 'on';
app.UIAxes.XMinorGrid = 'on';
app.UIAxes.YGrid = 'on';
app.UIAxes.YMinorGrid = 'on';
app.UIAxes.ButtonDownFcn = createCallbackFcn(app, @UIAxesButtonDown, true);
app.UIAxes.Position = [363 287 266 194];
% Create UIAxes2
app.UIAxes2 = uiaxes(app.UIFigure);
title(app.UIAxes2, ' Torque vs Speed')
xlabel(app.UIAxes2, 'Torque(Nm)')
ylabel(app.UIAxes2, 'Speed (rpm)')
zlabel(app.UIAxes2, 'Z')
app.UIAxes2.XGrid = 'on';
app.UIAxes2.XMinorGrid = 'on';
app.UIAxes2.YGrid = 'on';
app.UIAxes2.YMinorGrid = 'on';
app.UIAxes2.Position = [40 297 247 184];
% 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.XMinorGrid = 'on';
app.UIAxes3.YMinorGrid = 'on';
app.UIAxes3.Position = [1 43 300 215];
% 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.XGrid = 'on';
app.UIAxes4.XMinorGrid = 'on';
app.UIAxes4.YGrid = 'on';
app.UIAxes4.YMinorGrid = 'on';
app.UIAxes4.Position = [311 43 318 215];
% Create ImportdataButton
app.ImportdataButton = uibutton(app.UIFigure, 'push');
app.ImportdataButton.ButtonPushedFcn = createCallbackFcn(app, @ImportdataButtonPushed, true);
app.ImportdataButton.Position = [242 257 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 = [60 257 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 = [401 257 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 = [98 10 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 = [418 10 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)
% 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

Accepted Answer

Cris LaPierre
Cris LaPierre on 11 Aug 2021
Edited: Cris LaPierre on 11 Aug 2021
The snippet you have shared is working just fine. It is later on in your code that something unexpected is happening.
This line of code is deleting every row in your table A
app.A = app.A(app.A.Eta1_1_Total_WT1 >= 0 & app.A.Eta1_1_Total_WT1 <= 100,:);
This is because all values of app.A.Eta1_1_Total_WT1 are negative, so nothing matches the criteria. Since app.A is now empty, the plot command is working, but there is nothing to plot.
As an aside, since you have already set up your title and axes labels in the app designer canvas, it is not necessary to include code for this in your callback function.
Commenting that line out gives me the following result.
Another aside. Since I didn't have the Predictive Maintenance Toolbox installed, I have replaced the time2num function with minutes. It seems to accomplish the same thing, with the added benefit that minutes is included in MATLAB.
app.A.Time=minutes(app.A.Time);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!