Reset axes in Matlab App Designer
19 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi,
I'm creating an arduino oscilloscope app and I'm trying to clear the data from the axes when finshed plotting, therefore the axes resets. However, I've tried a few ways but the graph doesn't change. I've attached a photo of my app and the code is shown below.
classdef OscilloscopeGUI < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Panel3 matlab.ui.container.Panel
ResetButton matlab.ui.control.Button
SaveDataButton matlab.ui.control.Button
StopButton matlab.ui.control.Button
StartPlotButton matlab.ui.control.Button
Panel2 matlab.ui.container.Panel
UIAxes matlab.ui.control.UIAxes
Panel matlab.ui.container.Panel
Panel_2 matlab.ui.container.Panel
ConnecttoHardwarefirstLabel matlab.ui.control.Label
ConnLamp matlab.ui.control.Lamp
BDiscon matlab.ui.control.Button
BCon matlab.ui.control.Button
HardwarePanel matlab.ui.container.Panel
GridLayout11 matlab.ui.container.GridLayout
PortList matlab.ui.control.DropDown
PortLabel matlab.ui.control.Label
Refresh matlab.ui.control.Button
end
properties (Access = private)
%Define Function Variables
s; %serial port
time = 0; %time of plotting
data = zeros(2,1); %data stored in array
count = 0;
GREEN_COLOR = [0 1 0]
RED_COLOR = [1 0 0]
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)
plotGrid = 'on';
min = 100 ;
max = 300;
delay = .01;
%Set up Plot
ax = app.UIAxes;
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 app.time <= 50
%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(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)
% stopasync(app.s)
% stop(app.data)
% fclose(app.s);
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)
stopasync(app.s)
fclose(app.s);
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)
app.data = 0;
app.time=0;
app.count =0;
clear 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 -52 546 817];
% Create HardwarePanel
app.HardwarePanel = uipanel(app.Panel);
app.HardwarePanel.Title = 'Hardware';
app.HardwarePanel.Position = [10 614 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 708 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 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
5 comentarios
Elvis Pandzic
el 27 de En. de 2023
Hi Ashleigh,
Not sure if it will work for you but did for me...
try: cla(axes,'reset')
Respuestas (1)
Eric Delgado
el 27 de En. de 2023
You shouldn't clear the axes because it's gonna turn down the efficiency of your app. Just create a "startup" for the plot and a property of your app to keep the handle to this new line.
ydata = randn(1001,1);
app.lineHandle = plot(ydata);
Create a function that update only the YData property of the line.
ydata_new = randn(1001,1);
app.lineHandle.YData = ydata_new;
0 comentarios
Ver también
Categorías
Más información sobre Develop uifigure-Based Apps 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!