Cell array saving in real-time

조회 수: 2 (최근 30일)
Johnny Scalera
Johnny Scalera 2020년 7월 2일
댓글: Johnny Scalera 2020년 7월 8일
Hi all,
I'm pretty new in Matlab. I'm trying to save in real-time data received from sensors. An event manages the sensor data transmission, which is represented by the update of a data package: each time the event is called, a new data package is available. The number of samples constituting the package is variable in time.
In the following script, I simulate three packages of different number of samples (scanNumber), and two selected sensors from the users (sensorNum). Each sensor gives four different measures (blockSensor).
scanNumber = [10 11 9]; %number of samples for each package
blockSensor = 4; %each sensor produces 4 different measures
sensorNum = 2; %number of sensor selected by the user
measure1 = cell(sensorNum, 1);
measure2 = cell(sensorNum, 1);
measure3 = cell(sensorNum, 1);
measure4 = cell(sensorNum, 1);
saveCell = cell(1, blockSensor * sensorNum);
for package = 1:1:length(scanNumber)
% the following four lines simulate sensor readings
q = 1*ones(scanNumber(package), 4);
a = 2*ones(scanNumber(package), 3);
g = 3*ones(scanNumber(package), 3);
m = 4*ones(scanNumber(package), 3);
for sensor = 1:1:sensorNum
% data association from sensor readings to measurements arrays
measure1{sensor} = q;
measure2{sensor} = a;
measure3{sensor} = g;
measure4{sensor} = m;
% saving array
saveCell{-3+(sensor*blockSensor)} = measure1{sensor};
saveCell{-2+(sensor*blockSensor)} = measure2{sensor};
saveCell{-1+(sensor*blockSensor)} = measure3{sensor};
saveCell{ (sensor*blockSensor)} = measure4{sensor};
end
% SAVE THE PACKAGE HERE
end
I would like to save the cell array (saveCell) in a file in an efficient way minimizing at maximum the time consumption for the writing operation and minimizing the space occupied on the disk from the file. I need to append the new values in saveCell to its old values already saved in the file, every time a new package is available.
Thank you all,
Johnny
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 7월 8일
The logic you post simulates fetching all of the data, and then loops through the number of sensors writing the same infromation to each one. Are you sure that is representative of your work flow?
It would make more sense if you had one of the following possibilities:
  1. Read from all devices, but use the sensor number loop to write out only the data relevant to that sensor number. For example if sensor #3 was selected you might write out g and otherwise not write out g; OR
  2. Loop over the active sensors, reading the q, a, g, m from the specific sensor, writing it out, and not reading from any sensor that is not selected.
Johnny Scalera
Johnny Scalera 2020년 7월 8일
Hi Walter,
thanks for your reply. You are right, I am writing the same information for all sensors at each loop, which represents an unreal condition. But it doesn't matter because I would like to simulate the save block functionality.
Fetching all the data is exactly what I need. This code block will be inserted in a data acquisition tool that works in real-time. The two solutions You proposed are, in my opinion, more suitable for data visualization for example.
In my case, I want to read and save all the data from all the active sensors at each loop.
So, I'm trying to save data in a .txt file using the fprintf function
for package
for sensor
end
fprintf(fid, '%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f\n',...
[saveCell{:}].');
end
This code produces exactly what I want. The problem is the formatSpec. Each sensor gives 4 different measures for a total of 13 columns. In this example the user selected only two sensors. How could I specify the formatSpec in case the user selected 10 sensors?

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

답변 (1개)

Voss
Voss 2020년 7월 2일
Will save work?
  댓글 수: 3
Voss
Voss 2020년 7월 2일
You can maybe try fprintf with the contents of the cells of the cell array, which are numeric.
Johnny Scalera
Johnny Scalera 2020년 7월 8일
I tried but I still have some troubles. Each sensor produces 4 different measurements (in the previous script q, a, g, m), with a total of 13 columns arrays. Each column has the same number of samples equal to the 'scanNumber'. I would like to save data in this way
SENSOR 1 SENSOR 2 SENSOR N
13 columns 13 columns 13 columns
and append new data to the correct coulmn.
How can I use fprintf properly?

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

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by