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 件のコメント

Walter Roberson
Walter Roberson 2018 年 5 月 24 日
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 2018 年 5 月 24 日
just ignore it if the 53 is followed by something other 0,1,2
dpb
dpb 2018 年 5 月 24 日
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 2018 年 5 月 24 日
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

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2018 年 5 月 24 日

1 投票

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 件のコメント

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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by