Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
byte swapping 16-bit data

Subject: byte swapping 16-bit data

From: JSharer@psu.edu "Jack Sharer"

Date: 30 Oct, 1997 22:38:58

Message: 1 of 2

Hi,

I'm using fread to input 16-bit binary data into
MatLab 5 on a Sun. The data needs to be byte swapped.

In looking at the swapping in fopen, I don't see
a keyword for byte swapping 16-bit data. Am I
overlooking something?

Are there any MatLab functions which would make
this job easy (I have several gigabytes of data
to process).

Thanks, Jack W. Sharer, JSharer@psu.edu

Subject: byte swapping 16-bit data

From: daf@mathworks.com (David Foti)

Date: 31 Oct, 1997 00:18:48

Message: 2 of 2

Hi Jack,

You can use the fopen command to specify how to treat the file you are
reading (as little-endian or big-endian). If you choose the opposite
byte ordering from the machine you are reading the file into, then
MATLAB will automatically do the byte-swapping for you. For example
if you are using a PC (intel) which is a little-endian machine, you
could call fopen like this:

>>f = fopen('filename', 'r', 'ieee-be');

The byte swapping will occur on all data types including 16-bit data.
Read your data by using:

>>A = fread(f, [m,n], 'uint16')

to read 16 bit data into an m-by-n matrix.

Hope this helps,
-Dave

Jack Sharer (JSharer@psu.edu) wrote:
: Hi,
:
: I'm using fread to input 16-bit binary data into
: MatLab 5 on a Sun. The data needs to be byte swapped.
:
: In looking at the swapping in fopen, I don't see
: a keyword for byte swapping 16-bit data. Am I
: overlooking something?

-----------------------------------------------------
Dave Foti daf@mathworks.com
The MathWorks, Inc.
24 Prime Park Way
Natick, MA 01760 http://www.mathworks.com
-----------------------------------------------------

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us