MATLAB Answers

Storing data in a real time recording gui using a callback

5 views (last 30 days)
Hi,
I have created a GUI to read wavelength data from FBG fibers and reconstruct their shape in real time. To read, I use a TCPIP connection and a callback implemented with a terminatror. This terminator just throws an event when the new-line character is present in the data:
h = animatedline(app.UIAxes);
app.tcpFBG.BytesAvailableFcn = {@readingData,app,h};
app.tcpFBG.BytesAvailableFcnMode = 'Terminator';
app.tcpFBG.Terminator = 'LF';
In the callback "readingData" I read the data from the buffer, reconstruct the shape, store the data and plot the shape of the fibers
function data = readingData(connection,~,app,h)
tic
clearpoints(h)
data = fscanf(connection);
[EngineeredValues,PeakWavelengths,~] = app.fbg.readingData(data);
[shape,curv,curvDir] = app.fbg.reconstruct(EngineeredValues,PeakWavelengths);
app.pushShapeData(shape); %Saving the data in app.shapeData
app.pushCurvData(curv); %Saving the data in app.curvData
app.pushCurvDirData(curvDir); %Saving the data in app.curvDirData
addpoints(h,shape(1,:),shape(2,:),shape(3,:))
toc
end
The functions used to add the read data with data from previous samples is:
function pushShapeData(app,shape)
app.shapeData = cat(3,app.shapeData,shape);
end
function pushCurvData(app,curv)
app.curvData = cat(3,app.curvData,curv);
end
function pushCurvDirData(app,curvDir)
app.curvDirData = cat(3,app.curvDirData,curvDir);
end
I guess this way of storing the data is not efficient, and as the amount of data increases, this operation becomes slower. This problem makes my reconstruction and plotting laggy for long reading times, but as you can see, I don't know the total amount of data to be save because it depends on when the user stops the recording. Is there any efficient way to concatenate these array and store the data efficiently? Thank you in advance.

  3 Comments

Mohammad Sami
Mohammad Sami on 1 Feb 2020
Have you tried profiling. Which functions are taking the longest ?
Carlos Fambuena
Carlos Fambuena on 1 Feb 2020
Hi,
No I haven't, because I don't get any problems with this code at the beginning of the recording. I have checked that the cat operation takes more time when the arrays to concatenate are bigger. I used this code for that:
clear; clc;
DlgH = figure;
H = uicontrol('Style', 'PushButton', ...
'String', 'Break', ...
'Callback', 'delete(gcbf)');
a = zeros(3,33);
A = [];
while ishandle(H)
tic
A = cat(3,A,a);
toc
pause(0.02)
end
I am quite sure that is this operation what is giving me troubles, but I don't see what can I do instead.

Sign in to comment.

Accepted Answer

Mohammad Sami
Mohammad Sami on 1 Feb 2020
Instead of concatenating the data with every iteration, just store the data in a cell array. You can concatenate it when you need it.
% cell method
tic;
a = zeros(3,33);
A = cell(0,1); % empty cell array to store the data
for i = 1:10000
A{end+1} = a; % store in the cell array
end
A = cat(3,A{:}); % concatenate at the end when you need to.
toc;
%Elapsed time is 0.023587 seconds.
% current method
tic;
a = zeros(3,33);
A = [];
for i = 1:10000
A = cat(3,A,a);
end
toc;
%Elapsed time is 11.293808 seconds.

  0 Comments

Sign in to comment.

More Answers (0)

Sign in to answer this question.