App Designer serial runs

3 次查看(过去 30 天)
Hi,
I am still struggling through app designer and i still cannot get my app to run again. I have attached the code but I am at a road block and need help trouble shooting. I currently am hard coding hte arduino connection in so it has to be within my while loop. I basically want it to start again and draw another line but after it pops up the figure it will not. Any help is greatly appreciated!
classdef app2 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
RUNButton matlab.ui.control.Button
STOPButton matlab.ui.control.Button
UIAxes matlab.ui.control.UIAxes
COMPortEditFieldLabel matlab.ui.control.Label
COMPortEditField matlab.ui.control.EditField
SaveAsEditFieldLabel matlab.ui.control.Label
SaveAsEditField matlab.ui.control.EditField
end
properties (Access = private)
stop;
port;
done=0;
timeLogs;
forceLogs;
timeSecs;
filename;
T;
h;
a;
starttime;
t;
end
methods (Access = private)
function reset(app)
app.h=0;
app.starttime=0;
app.t=0;
app.stop=false;
end
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app, a)
app.a=arduino('Com5','uno');
end
% Value changed function: COMPortEditField
function COMPortEditFieldValueChanged(app, event)
value = app.COMPortEditField.Value;
app.port=value;
end
% Button pushed function: RUNButton
function RUNButtonPushed(app, event)
figure
app.h=animatedline;
ax=gca;
ax.YGrid='on';
ax.YLim=[0 80];
app.stop=false;
app.starttime=datetime('now');
while ~app.stop
% Read current voltage value
voltage1 = readVoltage(app.a,'A0');
voltage2 = readVoltage(app.a,'A1');
% Calculate force from voltage (based on data sheet)
% Flipped for ease of use
force1=-(13.499*voltage1)+68.534; %from calibration with standard weights
force2=-(9.9586*voltage2)+49.882; %from calibration with standard weights
force=force1+force2;
% Get current time
app.t = datetime('now') - app.starttime;
% Add points to animation
addpoints(app.h,datenum(app.t),force)
% Update axes
ax.XLim = datenum([app.t-seconds(15) app.t]);
datetick('x','keeplimits')
drawnow
% Check stop condition
app.stop = app.done;
end
end
% Button pushed function: STOPButton
function STOPButtonPushed(app, event)
app.done=1;
[app.timeLogs,app.forceLogs] = getpoints(app.h);
app.timeSecs = (app.timeLogs-app.timeLogs(1))*24*3600;
app.T = table(app.timeSecs',app.forceLogs','VariableNames',{'Time_sec','Force_N'});
% Write table to file
writetable(app.T,app.filename)
[app.timeLogs,app.forceLogs] = getpoints(app.h);
app.timeSecs = (app.timeLogs-app.timeLogs(1))*24*3600;
plot(app.UIAxes,app.timeSecs,app.forceLogs)
reset(app)
end
% Value changed function: SaveAsEditField
function SaveAsEditFieldValueChanged(app, event)
value = app.SaveAsEditField.Value;
app.filename=value;
end
% Value changing function: COMPortEditField
function COMPortEditFieldValueChanging(app, event)
changingValue = event.Value;
app.port=changingValue;
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
% Create RUNButton
app.RUNButton = uibutton(app.UIFigure, 'push');
app.RUNButton.ButtonPushedFcn = createCallbackFcn(app, @RUNButtonPushed, true);
app.RUNButton.BackgroundColor = [0 1 0];
app.RUNButton.Position = [388 22 100 31];
app.RUNButton.Text = 'RUN';
% Create STOPButton
app.STOPButton = uibutton(app.UIFigure, 'push');
app.STOPButton.ButtonPushedFcn = createCallbackFcn(app, @STOPButtonPushed, true);
app.STOPButton.BackgroundColor = [1 0 0];
app.STOPButton.Position = [509 22 100 31];
app.STOPButton.Text = 'STOP';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Force vs. Time')
xlabel(app.UIAxes, 'Time')
ylabel(app.UIAxes, 'Force')
app.UIAxes.YGrid = 'on';
app.UIAxes.Position = [11 72 619 398];
% Create COMPortEditFieldLabel
app.COMPortEditFieldLabel = uilabel(app.UIFigure);
app.COMPortEditFieldLabel.HorizontalAlignment = 'right';
app.COMPortEditFieldLabel.Position = [44 52 59 22];
app.COMPortEditFieldLabel.Text = 'COM Port';
% Create COMPortEditField
app.COMPortEditField = uieditfield(app.UIFigure, 'text');
app.COMPortEditField.ValueChangedFcn = createCallbackFcn(app, @COMPortEditFieldValueChanged, true);
app.COMPortEditField.ValueChangingFcn = createCallbackFcn(app, @COMPortEditFieldValueChanging, true);
app.COMPortEditField.Position = [118 52 100 22];
app.COMPortEditField.Value = 'Com';
% Create SaveAsEditFieldLabel
app.SaveAsEditFieldLabel = uilabel(app.UIFigure);
app.SaveAsEditFieldLabel.HorizontalAlignment = 'right';
app.SaveAsEditFieldLabel.Position = [45 12 54 22];
app.SaveAsEditFieldLabel.Text = 'Save As:';
% Create SaveAsEditField
app.SaveAsEditField = uieditfield(app.UIFigure, 'text');
app.SaveAsEditField.ValueChangedFcn = createCallbackFcn(app, @SaveAsEditFieldValueChanged, true);
app.SaveAsEditField.Position = [114 12 214 22];
end
end
methods (Access = public)
% Construct app
function app = app2(varargin)
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @(app)startupFcn(app, varargin{:}))
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

采纳的回答

Adam Danz
Adam Danz 2019-3-11
编辑:Adam Danz 2019-3-12
In general, always specify axis handles when plotting. This is especially important when creating plots from a GUI. The reason your data aren't appearing on the plot is because animatedline() doesn't know what axes to use. If you specify the axis handle, the problem will be solved. So you'll need to create the axes before creating the animaledline object.
figure
ax = axes; %don't use gca, it's not specific enough for GUIs.
app.h=animatedline(ax);
  3 个评论
Adam Danz
Adam Danz 2019-3-12
" I basically want it to start again and draw another line but after it pops up the figure it will not."
My solution fixes that problem. To test my solution, I changed your code so that the axis handle is an input to animatedline() (as shown in my solution above), I commented-out the voltage1 and voltage2 lines, and I gave 'force' a constant value. When I run your app with those changes, the data are correctly drawn on the newly created figure.
It sounds like you now have a new problem. Perhaps your voltage input is malfunctional?
Allison McCrady
Allison McCrady 2019-3-12
I got it! It wasn't resetting the done back to 0 so it didn't realize that it was time to go again! Thank you so much for your help! I really think it's starting to make sense! This is the new code at the top of the RunButton Pushed.
app.done=0;
app.stop=false;
app.starttime=datetime('now');
figure
ax=axes;
app.h=animatedline(ax);
ax.YGrid='on';
ax.YLim=[0 80];

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by