How to handle large variables in workspace

3 次查看(过去 30 天)
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

采纳的回答

Chunru
Chunru 2021-7-28
编辑:Chunru 2021-8-2
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 个评论
Frederick Acquah
Frederick Acquah 2022-10-7
I tried this code and I am getting an error because the data type of signal is a double while data is a timetable.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Spectral Measurements 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by