Re: Changing the data type of an array
- From: "Randy Poe" <poespam-trap@xxxxxxxxx>
- Date: 25 Aug 2005 11:00:27 -0700
Fernando Gomez wrote:
> Hi,
>
> I have a very specific question. I am sure there must be a way to do
> this efficiently yet I have been unable to do so.
>
> I am using MATLAB to communicate with a remote instrument (logic
> analyzer). That instrument provides the result of the measurement as
> an array of unsigned bytes. But as I know that it is indeed an array
> of 16 bit signed integers (int16), I have to go through a data
> conversion process. To do the conversion, currently I am reading the
> byte array with a "for" loop which builds a new array whose length is
> have the original array length. As you can imagine, this is way too
> slow when the data measured is large. I wonder if there is a
> better/faster way of doing this with MATLAB (I bet there is, but I
> have been unable to find it). For instance, if I was doing this in C,
> I would just use an integer pointer that would point to the byte
> array. Any help would be appreciated.
If I understand what you're saying, what you get
is an array of 2N uint8's, and you want to interpret
every pair of bytes as a signed int16.
One quick and dirty way to do it would be to use
low-level file I/O. Write your byte array A to a file
with fwrite(fid, A, 'uint8') and read it back with
A = fread(fid, inf, 'int16').
You may have an endian issue doing it this way (the
byte order your machine expects for int16's is different
from the order in the file). I think you can use an
argument in FOPEN or FREAD to fix that if it occurs.
Other ways that occur to me involve using bitwise
calls like BITSHIFT and BITOR, the same way you'd
assemble bytes into words in C. Those are vectorized
so you can do the whole array at once.
- Randy
.
- References:
- Changing the data type of an array
- From: Fernando Gomez
- Changing the data type of an array
- Prev by Date: Re: Virtual Memory While Making a Movie
- Next by Date: Re: The minimum 'TimerPeriod'
- Previous by thread: Changing the data type of an array
- Next by thread: searching
- Index(es):
Relevant Pages
|