MATLAB Answers

how can I find a short int array from a long int array

3 views (last 30 days)
wenlong
wenlong on 30 Apr 2017
Commented: wenlong on 4 May 2017
Now I have a long int array including some short array such beginning with 55 and ending 00, whose length was 10, but the contents between 55 and 00 could not be recognized for they were send from serial port. How could I extract those short arrays from the long array? Thanks!

  4 Comments

Show 1 older comment
wenlong
wenlong on 30 Apr 2017
I just read all the datas into a txt file, then I read it and got the long array! Thanks
Walter Roberson
Walter Roberson on 30 Apr 2017
How are you doing the reading from the text file? The details might matter.
wenlong
wenlong on 30 Apr 2017
I had a stm32f0, it just send datas from serial port to pc, I did not use any protocol

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 Apr 2017
as_short = typecast(YourLongArray, 'uint16');
sub_pos = find(as_short(1:end-9) == 55 & as_short(10:end) == 0);
short_array = as_short(sub_pos:sub_pos+9);

  8 Comments

Show 5 older comments
wenlong
wenlong on 3 May 2017
I thought I found the reason, first I shoud connect the port, then run the m file, at last power on the mcu so it sent datas and the m file read datas. I did not know why
Walter Roberson
Walter Roberson on 3 May 2017
You would normally need to write a protocol that triggers the mcu to send data, instead of just immediately sending it.
Perhaps you were thinking that when the MCU opens its serial port that the MCU would then wait untl MATLAB opens the serial port on its side before the MCU would proceed to send the data. It is possible to program serial ports that way, but it is not typically done on instruments.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by