How to handle large variables in workspace

조회 수: 3 (최근 30일)
진환 유
진환 유 2021년 7월 28일
댓글: Frederick Acquah 2022년 10월 7일
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
Chunru
Chunru 2021년 8월 2일
Nice to learn that.
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에서 Signal Generation, Manipulation, and Analysis에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by