getting data from an array correctly

Hi, I have the following data that I get it from the socket
header = [53 0 1 2 3 4 5 6 7 8 9 53 1 24 59 5 95 95 53 2 59 58 575 75 53 0 1 4 2 4 9 1];
The header first starts with 53, then maybe 0 or 1 or 2. so I need to get the bytes after 53 + 0 till the next 53 ( in between) and also get the data after 53 + 1 till the next 53 and so on
I tried that but it crashes
header = [53 0 1 2 3 4 5 6 7 8 9 53 1 24 59 5 95 95 53 2 59 58 575 75 53 0];
where = find(header == 53);
if numel(where) > 1 && header(where(1)+1) == 1 % check if there are more then 1 53s and number after first 53 is 0.
nextbytes = header(where(1)+2:where(2)-1);
disp(nextbytes);
end

4 comentarios

Walter Roberson
Walter Roberson el 24 de Mayo de 2018
If the data itself included a 53, then how would that be coded?
What do you want to do in the case of a 53 followed by something that is not a 0, 1, or 2 ?
Ahmed Tolba
Ahmed Tolba el 24 de Mayo de 2018
just ignore it if the 53 is followed by something other 0,1,2
dpb
dpb el 24 de Mayo de 2018
Is it possibly data, though, even if not new record marker?
A formal description of the protocol would be more beneficial, methinks.
Ahmed Tolba
Ahmed Tolba el 24 de Mayo de 2018
the protocal always start with 53 then its followed by 0 or 1 or 2 or 3, then it continues adding bytes after 53 1,2,3.. so its like header 53 and which ADC signal which is 0,1,2,3

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 24 de Mayo de 2018
where = [find(header(1:end-1)==53 & ismember(header(2:end), [0 1 2 3])), length(header)+1];
lengths = diff(where);
bytesplits = mat2cell(header(where(1):end), 1, lengths);
bytesplits is now a cell array in which each entry is a vector starting with an appropriate header.

3 comentarios

Ahmed Tolba
Ahmed Tolba el 24 de Mayo de 2018
Thanks, how to get the bytesplits, like the first section from 53 0 xxxx 53 1 xxx
Ahmed Tolba
Ahmed Tolba el 24 de Mayo de 2018
I mean convert bytesplits into vectors so that I can plot them
for K = 1 : length(bytesplits)
plot(bytesplits{K}(3:end), 'DisplayName', sprintf('#%d', K));
hold on
end
hold off
legend('show');

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 24 de Mayo de 2018

Comentada:

el 24 de Mayo de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by