find data in array
Show older comments
hi, according to an answer header = fread(ropaRadarReceive,bytesToRead);
where = find(header == 53);
if ~isempty(where)
%at least one 53 found
nextbytes = header(where + 1); %note that if more than one 53 is found, nextbytes will be a vector
%...
end
now I have found 53, that header can be followed by 0 or 1 or 2 or 3, OR normal bytes other than those values.
I want to test if the next byte of 53 is 0 or 1 or 2 or 3, if that's true, I want to get the bytes after 0 or 1 or 2 or 3. UNTILL the next header is found which is 53.
Please help me
Answers (2)
Ameer Hamza
on 24 May 2018
Edited: Ameer Hamza
on 24 May 2018
If more then one 53 are found then where will be a vector and you need to use it like this
nextbytes = header(where(1)+1:where(2)-1);
7 Comments
Ahmed Tolba
on 24 May 2018
Ameer Hamza
on 24 May 2018
Try this
if numel(where) > 1 & header(where(1)+1) == 0 % check if there are more then 1 53s and number after first 53 is 0.
nextbytes = header(where(1)+2:where(2)-1);
end
Ahmed Tolba
on 24 May 2018
Edited: Ahmed Tolba
on 24 May 2018
Ameer Hamza
on 24 May 2018
If you can give an example of your data, then it will be easier to suggest a solution.
Ahmed Tolba
on 24 May 2018
Edited: Ahmed Tolba
on 24 May 2018
Ahmed Tolba
on 24 May 2018
Ahmed Tolba
on 24 May 2018
Andrei Bobrov
on 24 May 2018
Edited: Andrei Bobrov
on 24 May 2018
i1 = find(header(:)==53);
p = hankel(header(1:end-1),header(end-1:end));
i2 = find(ismember(p,[53*ones(4,1),(0:3)'],'rows'));
[~,aa] = setdiff(i1,i2);
i1 = i1(aa) + 1;
i2 = i2 + 2;
out = header(sort([i1(i1 < numel(header));i2(i2 < numel(header))]));
use
>> 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]
header =
Columns 1 through 18
53 0 1 2 3 4 5 6 7 8 9 53 1 24 59 5 95 95
Columns 19 through 26
53 2 59 58 575 75 53 0
>> i1 = find(header(:)==53);
p = hankel(header(1:end-1),header(end-1:end));
i2 = find(ismember(p,[53*ones(4,1),(0:3)'],'rows'));
[~,aa] = setdiff(i1,i2);
i2 = i2 + 2;
out = header(sort([i1(aa)+1;i2(i2 < numel(header))]))
out =
1 24 59
>>
1 Comment
Ahmed Tolba
on 24 May 2018
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!