# Problems to arrange incoming Data in array

1 view (last 30 days)
Simon Schirk on 19 Apr 2024
Answered: SAI SRUJAN on 1 May 2024
I am using Simulink on a Raspberry Pi and request Data from an Arduino via Serial Communication and Receive an 18x1 uint8 array. I want to split the Array into 6 different double variables and wirte them into an n x 5 array.
Since the Pi receives a continious stream of Data I have a * at the beginnig of my array to determine that there is data in that array. It looks like that:
The MATLAB function in my Simulink model that handles the data has following code:
persistent counter;
persistent NachrichtAusgelesenTmp;
persistent FahrspurBuffer;
if isempty(counter)
counter = 0;
NachrichtAusgelesenTmp = zeros(500,5);
FahrspurBuffer = zeros(500,5);
end
% Setup
Geschwindigkeit = 0;
Fahrspur = zeros(1,5);
% read and reconditioning of the data
counter = counter + 1;
% binary into values
if Fahrtrichtung == 1
Geschwindigkeit = 0.28;
elseif Fahrtrichtung == 0
Geschwindigkeit = -0.28;
end
% some change in units
NachrichtAusgelesenTmp(counter,:) = [xKoordinate yKoordinate MaehwerkStatus Geschwindigkeit VorsteuerungKappaBahn];
counter = 0;
idx = find(NachrichtAusgelesenTmp(:,1));
FahrspurBuffer = NachrichtAusgelesenTmp(1:idx(end),:);
Fahrspur = FahrspurBuffer;
SpurFreigabe = true;
end
The Var Fahrspur should be nx5 and containing [xKoordinate yKoordinate MaehwerkStatus Geschwindigkeit VorsteuerungKappaBahn] in each row. The Data in the mat-Log file has all the values for one Var (e.g. xKoordinate) written in a single collum and somewhat later the same happens with the next Var. It's always the first collum and I can't figure out why the data is not sorted like I want it to be. Attached there is an example from my mat-file-logging.

SAI SRUJAN on 1 May 2024
Hi Simon,
I understand that you are facing an issue arranging the incoming data into an array.
Please go through the following code sample below that I utilized to verify the accuracy of the provided code sample.
persistent counter;
persistent FahrspurBuffer;
if isempty(counter)
counter = 0;
NachrichtAusgelesenTmp = zeros(5,5);
FahrspurBuffer = zeros(500,5);
end
% Setup
Geschwindigkeit = 0;
Fahrspur = zeros(1,5);
% read and reconditioning of the data
counter = counter + 1;
% binary into values
if Fahrtrichtung == 1
Geschwindigkeit = 0.28;
elseif Fahrtrichtung == 0
Geschwindigkeit = -0.28;
end
% some change in units
NachrichtAusgelesenTmp(counter,:) = [xKoordinate yKoordinate MaehwerkStatus Geschwindigkeit VorsteuerungKappaBahn];
counter
NachrichtAusgelesenTm=NachrichtAusgelesenTmp;
SpurFreigabe = false;
counter = 0;
idx = find(NachrichtAusgelesenTmp(:,1));
FahrspurBuffer = NachrichtAusgelesenTmp(1:idx(end),:);
Fahrspur = FahrspurBuffer;
SpurFreigabe = true;
NachrichtAusgelesenTm=NachrichtAusgelesenTmp;
end
end
ProcessedMessagesArray = zeros(5,5);
for i = [1:5]
[a, b, ProcessedMessagesArray] = fcn(MessageReceived, ProcessedMessagesArray);
end
counter = 1
counter = 2
counter = 3
counter = 4
counter = 5
ProcessedMessagesArray
ProcessedMessagesArray = 5x5
168.7100 304.3500 0 0.2800 0.5319 168.7100 304.3500 0 0.2800 0.5319 168.7100 304.3500 0 0.2800 0.5319 168.7100 304.3500 0 0.2800 0.5319 168.7100 304.3500 0 0.2800 0.5319
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
save('myArrayFile.mat', 'ProcessedMessagesArray');
It is evident that the variable "ProcessedMessagesArray" is accurately calculated, and the 'counter' is appropriately updated and saved into the MAT file, "myArrayFile.mat". To address the issue you're facing, please ensure that the 'elseif' statement correctly identifies the end of the dataset and that the data is being correctly written to the '.mat' file.
I hope this helps!

### Categories

Find more on Characters and Strings in Help Center and File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!