Filter löschen
Filter löschen

Cell array saving in real-time

1 Ansicht (letzte 30 Tage)
Johnny Scalera
Johnny Scalera am 2 Jul. 2020
Kommentiert: Johnny Scalera am 8 Jul. 2020
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 Kommentare
Walter Roberson
Walter Roberson am 8 Jul. 2020
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 am 8 Jul. 2020
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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Voss
Voss am 2 Jul. 2020
Will save work?
  3 Kommentare
Voss
Voss am 2 Jul. 2020
You can maybe try fprintf with the contents of the cells of the cell array, which are numeric.
Johnny Scalera
Johnny Scalera am 8 Jul. 2020
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?

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by