필터 지우기
필터 지우기

GUI Matlab real time data storage using DataAcquisition Interface

조회 수: 5 (최근 30일)
sherry
sherry 2024년 6월 9일
댓글: Mario Malic 2024년 6월 15일
Hope you all well, i have GUI app which is getting data from national instrument and give back live plot after trun switch on and when switch is off the recorded data wil stop and real time data will stop, however i am trying to intesert another button to clear the previous or delete them from store array however it won't happened i tried clear func and clf to remove plot and delete the array nothing working i will place the code blow and screen shot of the app design if you could help me to solve this i would be supre greatfull to you what i want is that the previouse data from block 1 and 2 get deleted after clear data button is clicked
classdef app2 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
TabGroup matlab.ui.container.TabGroup
Tab matlab.ui.container.Tab
CleardataButton matlab.ui.control.Button
DistancemmEditField matlab.ui.control.NumericEditField
DistancemmEditFieldLabel matlab.ui.control.Label
VoltageLEditField matlab.ui.control.NumericEditField
VoltageLEditFieldLabel matlab.ui.control.Label
LoadNEditField matlab.ui.control.NumericEditField
LoadNEditFieldLabel matlab.ui.control.Label
VoltageEditField matlab.ui.control.NumericEditField
VoltageEditFieldLabel matlab.ui.control.Label
SAVEDATAButton matlab.ui.control.Button
Lamp matlab.ui.control.Lamp
LampLabel matlab.ui.control.Label
Switch matlab.ui.control.Switch
SwitchLabel matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end
properties (Access = private)
dq = daq("ni"); % Create a DataAcquisition object.
R = 5000;% Description
% % Arrays to store data for plotting
% timeData = [];
% loadData = [];
% distanceData = [];
end
properties (Access = public)
voltage
distance
load
f1
f2
timeData = [];
loadData = [];
distanceData = [];
end
methods (Access = public)
%
function getdat(app,src,~)
[d, ~] = read(src,src.ScansAvailableFcnCount,"OutputFormat","Matrix");
voltage = mean(d(:,1));
app.VoltageEditField.Value = voltage;
app.LoadNEditField.Value = voltage * 4.19; %* 10497;
load =app.LoadNEditField.Value ;
app.VoltageLEditField.Value = mean(d(:,2));
distance = (app.VoltageLEditField.Value - 0.7966) * 4.51;
app.DistancemmEditField.Value = distance;
% Append data to arrays
app.timeData = [app.timeData; datetime('now')];
app.loadData = [app.loadData; load];
app.distanceData = [app.distanceData; distance];
% Plot all data
yyaxis(app.UIAxes, 'left');
app.f1= plot(app.UIAxes, app.timeData, app.loadData);
ylabel(app.UIAxes, 'Load (N)');
yyaxis(app.UIAxes, 'right');
app.f2= plot(app.UIAxes, app.timeData, app.distanceData, 'r');
ylabel(app.UIAxes, 'Displacement (mm)');
xlabel(app.UIAxes, 'Time');
app.UIAxes.XGrid = 'on';
app.UIAxes.YGrid = 'on';
%{
plot(app.UIAxes, app.timeData, app.loadData, 'b', app.timeData, app.distanceData, 'r');
xlabel(app.UIAxes, 'Time');
ylabel(app.UIAxes, 'Load (N) / Distance (mm)');
legend(app.UIAxes, 'Load', 'Distance');
%}
end
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: Switch
function SwitchValueChanged(app, event)
value = app.Switch.Value;
if strcmp(value,"On")
app.Lamp.Color = 'g';
app.dq = daq("ni");
app.dq.ScansAvailableFcn = @(src,event) getdat(app, src, event);
ch1 = addinput(app.dq, 'Dev1', 'ai0','Voltage');
ch2 = addinput(app.dq, 'Dev1', 'ai1','Voltage');
app.dq.Rate = app.R;
app.dq.UserData = [0 0];
start(app.dq,"Continuous");
else
% app.dq.ScansAvailableFcn = @(src,event) getdat(app, src, event);
stop(app.dq);
app.Lamp.Color = 'r';
% delete(app.f1);
%
% delete(app.f2);
removechannel(app.dq,1:length(app.dq))
% start(app.dq,"Continuous");
% removechannel(app.dq,2)
% daqreset
end
end
% Button pushed function: SAVEDATAButton
function SAVEDATAButtonPushed(app, event)
% Prompt the user to choose a filename and location for saving the data
[filename, filepath] = uiputfile('*.csv', 'Save Data As');
if isequal(filename,0) || isequal(filepath,0)
% User canceled the operation
return;
end
% Combine the filename and path
fullpath = fullfile(filepath, filename);
% Open or create the CSV file
fid = fopen(fullpath, 'w');
% Write headers to the CSV file
fprintf(fid, 'Time,Load (N),Displacement (mm)\n');
% Write data to the CSV file
for i = 1:numel(app.timeData)
fprintf(fid, '%s,%f,%f\n', ...
datestr(app.timeData(i)), ...
app.loadData(i), ...
app.distanceData(i));
end
% Close the file
fclose(fid);
end
% Callback function
function SystemLogSizeChanged(app, event)
% position = app.SystemLog.Position;
% if strcmp(app.Lamp.Color,'g')
%
end
% Button pushed function: CleardataButton
function CleardataButtonPushed(app, event)
% app.dq.ScansAvailableFcn = @(src,event) getdat(app, src, event);
stop(app.dq);
% Clear the session and channels.
% app.timeData = [app.timeData; datetime('now')];
% app.loadData = [app.loadData; app.load];
% app.distanceData = [app.distanceData; app.distance];
delete(app.f1);
delete ('app.timeData')
delete ('app.distanceData')
delete ('app.loadData')
delete(app.f2);
% daqreset
% start(app.dq);
% app.dq
% stop(app.dq);
% cla(app.load,"reset");
% delete (app.load);
% cla(app.distance,"reset");
% delete(app.distance);
% cla(app.UIAxes);
% cla()
% delete(app.dq);
% delete(app.distance);
% delete(app.UIAxes)
% cla(app.UIAxes,"reset");
% app.dq.release('ni');
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 1051 699];
app.UIFigure.Name = 'MATLAB App';
% Create TabGroup
app.TabGroup = uitabgroup(app.UIFigure);
app.TabGroup.Position = [1 0 1051 699];
% Create Tab
app.Tab = uitab(app.TabGroup);
app.Tab.Title = 'Tab';
% Create UIAxes
app.UIAxes = uiaxes(app.Tab);
xlabel(app.UIAxes, 'Displacement (mm)')
ylabel(app.UIAxes, 'Force (N)')
zlabel(app.UIAxes, 'Z')
app.UIAxes.YGrid = 'on';
app.UIAxes.Position = [15 26 828 505];
% Create SwitchLabel
app.SwitchLabel = uilabel(app.Tab);
app.SwitchLabel.HorizontalAlignment = 'center';
app.SwitchLabel.Position = [40 599 40 22];
app.SwitchLabel.Text = 'Switch';
% Create Switch
app.Switch = uiswitch(app.Tab, 'slider');
app.Switch.ValueChangedFcn = createCallbackFcn(app, @SwitchValueChanged, true);
app.Switch.Position = [37 636 45 20];
% Create LampLabel
app.LampLabel = uilabel(app.Tab);
app.LampLabel.HorizontalAlignment = 'right';
app.LampLabel.Position = [25 562 35 22];
app.LampLabel.Text = 'Lamp';
% Create Lamp
app.Lamp = uilamp(app.Tab);
app.Lamp.Position = [75 562 20 20];
app.Lamp.Color = [1 0 0];
% Create SAVEDATAButton
app.SAVEDATAButton = uibutton(app.Tab, 'push');
app.SAVEDATAButton.ButtonPushedFcn = createCallbackFcn(app, @SAVEDATAButtonPushed, true);
app.SAVEDATAButton.Position = [940 25 100 23];
app.SAVEDATAButton.Text = 'SAVE DATA';
% Create VoltageEditFieldLabel
app.VoltageEditFieldLabel = uilabel(app.Tab);
app.VoltageEditFieldLabel.HorizontalAlignment = 'right';
app.VoltageEditFieldLabel.Position = [878 226 46 22];
app.VoltageEditFieldLabel.Text = 'Voltage';
% Create VoltageEditField
app.VoltageEditField = uieditfield(app.Tab, 'numeric');
app.VoltageEditField.Position = [939 226 100 22];
% Create LoadNEditFieldLabel
app.LoadNEditFieldLabel = uilabel(app.Tab);
app.LoadNEditFieldLabel.HorizontalAlignment = 'right';
app.LoadNEditFieldLabel.Position = [872 133 52 22];
app.LoadNEditFieldLabel.Text = 'Load (N)';
% Create LoadNEditField
app.LoadNEditField = uieditfield(app.Tab, 'numeric');
app.LoadNEditField.Position = [939 133 100 22];
% Create VoltageLEditFieldLabel
app.VoltageLEditFieldLabel = uilabel(app.Tab);
app.VoltageLEditFieldLabel.HorizontalAlignment = 'right';
app.VoltageLEditFieldLabel.Position = [861 188 63 22];
app.VoltageLEditFieldLabel.Text = 'Voltage (L)';
% Create VoltageLEditField
app.VoltageLEditField = uieditfield(app.Tab, 'numeric');
app.VoltageLEditField.Position = [939 188 100 22];
% Create DistancemmEditFieldLabel
app.DistancemmEditFieldLabel = uilabel(app.Tab);
app.DistancemmEditFieldLabel.HorizontalAlignment = 'right';
app.DistancemmEditFieldLabel.Position = [841 82 83 22];
app.DistancemmEditFieldLabel.Text = 'Distance (mm)';
% Create DistancemmEditField
app.DistancemmEditField = uieditfield(app.Tab, 'numeric');
app.DistancemmEditField.Position = [939 82 100 22];
% Create CleardataButton
app.CleardataButton = uibutton(app.Tab, 'push');
app.CleardataButton.ButtonPushedFcn = createCallbackFcn(app, @CleardataButtonPushed, true);
app.CleardataButton.Position = [938 280 100 23];
app.CleardataButton.Text = 'Clear data';
% 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 = app2
% 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
  댓글 수: 2
Mario Malic
Mario Malic 2024년 6월 14일
Hi,
these are variables/properties
delete ('app.timeData')
delete ('app.distanceData')
delete ('app.loadData')
replace this code with
app.timeData = [];
% ...
aswell, to clear the axes, uncomment this line
cla(app.UIAxes);
Try and see if it works. If it doesn't upload the app file.
sherry
sherry 2024년 6월 14일
perfect , i have tried again and used the app it works thank you so much

댓글을 달려면 로그인하십시오.

답변 (1개)

Mario Malic
Mario Malic 2024년 6월 10일
Hi Sherry,
  댓글 수: 7
sherry
sherry 2024년 6월 15일
your app works it was mistake from my end so sorry for confusion
Mario Malic
Mario Malic 2024년 6월 15일
Amazing, thank you for letting me know.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Develop uifigure-Based Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by