How to handle large variables in workspace

5 views (last 30 days)
I have a code shown below which reads data from data aquisition object and updates variable signal and update time plot and specturm plot. the problem is that, as measurement goes along(which means very large size of variable signal), plot update speed and UIApp(created by app designer) performance get significantly slow. threshold timing depends on sampling frequency. In case of 51200Hz, I found it is approximately 40sec. but I need to measure data while 120sec... so how can I handle this large size of data generated in workspace?
n = ceil(app.daqDevice.Rate/10);
signal = [];
% start measurement
start(app.daqDevice,"continuous")
pause(0.1)
while(1)
% check measurement end condition
if app.Button_2.Value == 1
stop(app.daqDevice)
app.Button.Value = 0;
save(fullfile(app.SaveFolderPath,...
str2double(app.TrialNumber_Value.Text)+...
"_"+app.SpeedList.Value+".mat"),...
'signal')
return;
end
% read data form DataAquisition Obj
data = read(app.daqDevice,n);
% update measured value
signal = [signal; data];
% update time plot
plot(app.Timeplot,data.Time, data.Variables)
ylim(app.Timeplot,[-1,1])
% update spectrum plot
[pxx,f] = pwelch(data.Variables,512,256,1024,32000);
plot(app.PSDplot,f, 10*log10(pxx))
end

Accepted Answer

Chunru
Chunru on 28 Jul 2021
Edited: Chunru on 2 Aug 2021
Only process the latest data and throw away old data. You need to consider streaming processing of your data. See suggestions below in the code.
n = ceil(app.daqDevice.Rate/10);
%signal = [];
% Pre-allocate memory space for fixed amount of data
% This will make program faster
signal = zeros(nmax, 1); % nmax number of samples
% For data logging, consider fopen and fwrite for speed.
% start measurement
start(app.daqDevice,"continuous")
pause(0.1)
while(1)
% check measurement end condition
if app.Button_2.Value == 1
stop(app.daqDevice)
app.Button.Value = 0;
% save data block by block not in one-go
save(fullfile(app.SaveFolderPath,...
str2double(app.TrialNumber_Value.Text)+...
"_"+app.SpeedList.Value+".mat"),...
'signal')
return;
end
% read data form DataAquisition Obj
data = read(app.daqDevice,n);
% save data here using fwrite
% update measured value
signal(1:nmax-n) = singal(n+1:nmax); % shift data
signal(nmax-n+1:nmax) = data; % attach new data
%signal(nmax ) = [signal; data];
% update time plot
% plot the buffered signal instead of all signal
plot(app.Timeplot, data.Time, data.Variables)
ylim(app.Timeplot,[-1,1])
% compute spectrum on buffered data signal below
% update spectrum plot
[pxx,f] = pwelch(data.Variables,512,256,1024,32000);
plot(app.PSDplot,f, 10*log10(pxx))
end
  3 Comments
Frederick Acquah
Frederick Acquah on 7 Oct 2022
I tried this code and I am getting an error because the data type of signal is a double while data is a timetable.

Sign in to comment.

More Answers (0)

Categories

Find more on Signal Generation, Manipulation, and Analysis in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!