Stop data plotting in Matlab App Designer

9 visualizaciones (últimos 30 días)
Ashleigh Reid
Ashleigh Reid el 23 de Jul. de 2021
Respondida: Pavan Guntha el 28 de Jul. de 2021
Hi,
I'm trying to create an Arduino Oscilloscope in app designer, one of my functionalites is to have a 'stop' button so the user can stop the live plot at any time and observe the data. I've tried different ways such as:
fclose(app.s)
Nothing seems to work just yet as the data continues to plot infinitly, any help is appreciated.
properties (Access = private)
%Define Function Variables
s; %serial port
time ; %time of plotting
data = zeros(2,1); %data stored in array
count = 0;
GREEN_COLOR = [0 1 0]
RED_COLOR = [1 0 0]
plotGraph
end
methods (Access = private)
function closeApp_Callback(app, ~, event)
%closeApp_Callback Executes after "Close Confirm" dialog window
% "Close Confirm" dialog window is launched from AppCloseRequest.
switch event.SelectedOption
case 'OK'
% Disconnect from oscilloscope instrument
% (Execute DisconnectButtonPushed callback function)
BConButtonPushed(app,[])
delete(app)
case 'Cancel'
% Continue
end
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
% looking for available serial ports
% (Instrument Control Toolbox required)
p = instrhwinfo('serial');
app.PortList.Items = p.AvailableSerialPorts;
end
% Button pushed function: StartPlotButton
function StartPlotButtonPushed(app, event)
app.time =0;
plotGrid = 'on';
max = 300;
min =200;
delay = .01;
%Set up Plot
ax = app.UIAxes;
app.plotGraph = plot(ax, app.time,app.data(1,:),'-r');
hold(app.UIAxes,'on')
plotGraph1 = plot(ax, app.time,app.data(2,:),'-m');
hold(app.UIAxes,'off')
axis(ax, [ 0 10 min max]);
grid(ax, plotGrid);
% start stopwatch timer
tic
%Loop when Plot is Active
while ishandle(app.plotGraph)
%Read Data from Serial as Float
dat = fscanf(app.s,'%f');
if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct
app.count = app.count + 1;
app.time(app.count) = toc; %Extract Elapsed Time in seconds
app.data(:,app.count) = dat(:,1); %Extract 1st Data Element
set(app.plotGraph,'XData',app.time,'YData',app.data(2,:));
set(plotGraph1,'XData',app.time,'YData',app.data(1,:));
axis(ax,[0 app.time(app.count) min max]);
%Allow MATLAB to Update Plot
pause(delay);
end
end
end
% Button pushed function: SaveDataButton
function SaveDataButtonPushed(app, event)
t = table(app.data);
writetable(t,"test.xlsx","Sheet",1);
end
% Button pushed function: StopButton
function StopButtonPushed(app, event)
if app.time.running == "on"
stop(app.time);
stopasync(app.s)
fclose(app.s);
end
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
question = 'Close app?';
uiconfirm(app.UIFigure, question, 'Confirm Close',...
'CloseFcn',@(src,event) closeApp_Callback(app,src,event));
end
% Button pushed function: BCon
function BConButtonPushed(app, event)
% if strcmp(app.time.running, 'off')
app.s = serial(app.PortList.Value);
fopen(app.s);
% start(app.time);
app.ConnLamp.Color = app.GREEN_COLOR;
% end
end
% Button pushed function: BDiscon
function BDisconButtonPushed(app, event)
if app.time.running == "on"
stop(app.t);
if ~app.data
stopasync(app.s)
fclose(app.s);
end
app.Lamp.Color = app.RED_COLOR;
end
app.ConnLamp.Color = app.RED_COLOR;
end
% Button pushed function: Refresh
function RefreshButtonPushed(app, event)
% delete all serial port objets from MATLAB memory
% (Instrument Control Toolbox required)
delete(instrfind);
% looking for available serial ports
% (Instrument Control Toolbox required)
p = instrhwinfo('serial');
app.PortList.Items = p.AvailableSerialPorts;
end
% Button pushed function: ResetButton
function ResetButtonPushed(app, event)
cla(app.UIAxes)
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 1255 764];
app.UIFigure.Name = 'MATLAB App';
app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);
% Create Panel
app.Panel = uipanel(app.UIFigure);
app.Panel.Title = 'Panel';
app.Panel.Position = [2 2 546 763];
% Create HardwarePanel
app.HardwarePanel = uipanel(app.Panel);
app.HardwarePanel.Title = 'Hardware';
app.HardwarePanel.Position = [10 560 527 95];
% Create GridLayout11
app.GridLayout11 = uigridlayout(app.HardwarePanel);
app.GridLayout11.ColumnWidth = {'1x', '3x'};
app.GridLayout11.Padding = [5 5 5 5];
% Create Refresh
app.Refresh = uibutton(app.GridLayout11, 'push');
app.Refresh.ButtonPushedFcn = createCallbackFcn(app, @RefreshButtonPushed, true);
app.Refresh.Layout.Row = 2;
app.Refresh.Layout.Column = 2;
app.Refresh.Text = 'Refresh';
% Create PortLabel
app.PortLabel = uilabel(app.GridLayout11);
app.PortLabel.HorizontalAlignment = 'right';
app.PortLabel.Layout.Row = 1;
app.PortLabel.Layout.Column = 1;
app.PortLabel.Text = 'Port';
% Create PortList
app.PortList = uidropdown(app.GridLayout11);
app.PortList.Items = {'COM?'};
app.PortList.Editable = 'on';
app.PortList.BackgroundColor = [1 1 1];
app.PortList.Layout.Row = 1;
app.PortList.Layout.Column = 2;
app.PortList.Value = 'COM?';
% Create Panel_2
app.Panel_2 = uipanel(app.Panel);
app.Panel_2.Position = [12 654 525 80];
% Create BCon
app.BCon = uibutton(app.Panel_2, 'push');
app.BCon.ButtonPushedFcn = createCallbackFcn(app, @BConButtonPushed, true);
app.BCon.Position = [44 19 112 23];
app.BCon.Text = 'Connect';
% Create BDiscon
app.BDiscon = uibutton(app.Panel_2, 'push');
app.BDiscon.ButtonPushedFcn = createCallbackFcn(app, @BDisconButtonPushed, true);
app.BDiscon.Position = [227 19 124 23];
app.BDiscon.Text = 'Disconnect';
% Create ConnLamp
app.ConnLamp = uilamp(app.Panel_2);
app.ConnLamp.Position = [418 19 32 32];
app.ConnLamp.Color = [1 0 0];
% Create ConnecttoHardwarefirstLabel
app.ConnecttoHardwarefirstLabel = uilabel(app.Panel_2);
app.ConnecttoHardwarefirstLabel.FontWeight = 'bold';
app.ConnecttoHardwarefirstLabel.FontColor = [1 0 0];
app.ConnecttoHardwarefirstLabel.Position = [5 52 152 22];
app.ConnecttoHardwarefirstLabel.Text = 'Connect to Hardware first';
% Create y_axis_1_menu
app.y_axis_1_menu = uidropdown(app.Panel);
app.y_axis_1_menu.Items = {'Select...', 'channel 1', 'channel 2', 'channel 3', 'channel 4', 'channel 5', 'channel 6', 'channel 7', 'channel 8', 'channel 9'};
app.y_axis_1_menu.Tag = 'y_axis_1_menu';
app.y_axis_1_menu.FontSize = 10.6666666666667;
app.y_axis_1_menu.BackgroundColor = [0.729411764705882 0.831372549019608 0.956862745098039];
app.y_axis_1_menu.Position = [39 507 132 22];
app.y_axis_1_menu.Value = 'Select...';
% Create Panel2
app.Panel2 = uipanel(app.UIFigure);
app.Panel2.Title = 'Panel2';
app.Panel2.Position = [545 1 711 666];
% Create UIAxes
app.UIAxes = uiaxes(app.Panel2);
title(app.UIAxes, 'Oscilloscope')
xlabel(app.UIAxes, 'Time (s)')
ylabel(app.UIAxes, 'Data')
zlabel(app.UIAxes, 'Z')
app.UIAxes.XLim = [0 50];
app.UIAxes.YLim = [-1.5 1.5];
app.UIAxes.XTick = [0 5 10 15 20 25 30 35 40 45 50];
app.UIAxes.XGrid = 'on';
app.UIAxes.YGrid = 'on';
app.UIAxes.Position = [55 10 653 614];
% Create Panel3
app.Panel3 = uipanel(app.UIFigure);
app.Panel3.Title = 'Panel3';
app.Panel3.Position = [547 667 712 99];
% Create StartPlotButton
app.StartPlotButton = uibutton(app.Panel3, 'push');
app.StartPlotButton.ButtonPushedFcn = createCallbackFcn(app, @StartPlotButtonPushed, true);
app.StartPlotButton.BackgroundColor = [0.3294 0.9216 0.3294];
app.StartPlotButton.FontWeight = 'bold';
app.StartPlotButton.FontColor = [0.149 0.149 0.149];
app.StartPlotButton.Position = [70 16 100 47];
app.StartPlotButton.Text = 'Start Plot';
% Create StopButton
app.StopButton = uibutton(app.Panel3, 'push');
app.StopButton.ButtonPushedFcn = createCallbackFcn(app, @StopButtonPushed, true);
app.StopButton.BackgroundColor = [1 0 0];
app.StopButton.FontWeight = 'bold';
app.StopButton.FontColor = [0.149 0.149 0.149];
app.StopButton.Position = [218 18 100 47];
app.StopButton.Text = 'Stop';
% Create SaveDataButton
app.SaveDataButton = uibutton(app.Panel3, 'push');
app.SaveDataButton.ButtonPushedFcn = createCallbackFcn(app, @SaveDataButtonPushed, true);
app.SaveDataButton.BackgroundColor = [0.8 0.8 0.8];
app.SaveDataButton.FontWeight = 'bold';
app.SaveDataButton.FontColor = [0.149 0.149 0.149];
app.SaveDataButton.Position = [516 16 100 47];
app.SaveDataButton.Text = 'Save Data';
% Create ResetButton
app.ResetButton = uibutton(app.Panel3, 'push');
app.ResetButton.ButtonPushedFcn = createCallbackFcn(app, @ResetButtonPushed, true);
app.ResetButton.BackgroundColor = [0 1 1];
app.ResetButton.FontWeight = 'bold';
app.ResetButton.FontColor = [0.149 0.149 0.149];
app.ResetButton.Position = [360 18 100 47];
app.ResetButton.Text = 'Reset';
% 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 = OscilloscopeGUI
% 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

Respuestas (1)

Pavan Guntha
Pavan Guntha el 28 de Jul. de 2021
Hi Ashleigh,
I understand that the issue is regarding the integration of stop button with the uiaxes. You could look at the attached example app which illustrates the required behaviour.
Hope this helps!

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by