Rss Feed
Tweeter button
Facebook button
Technorati button
Reddit button
Myspace button
Linkedin button
Webonews button
Delicious button
Digg button
Flickr button
Stumbleupon button
Newsvine button
Youtube button

Notes:

  • The site will be morphing over the next little while.
  • I am having some issues with tabs/spaces in the Python code. Sometimes they are correct, sometimes they get eaten. I am trying to figure it out.

Please feel free to leave a comment on a post if it interests you or if you have questions

Feb 012010

One of the most common things I do in Matlab almost always involves reading in binary data.  For a few years I went through the typical fp=fopen('filename.dat'....  After typing the fopen, fread, reshape and fclose too many times, I finally made it into an all-in-one Matlab function called readraw() which will do all the reading and reformatting in one function call.

1
2
3
4
5
6
7
8
9
10
11
function [d] = readraw(filename, type, ds, endian)

if( nargin == 3 )
    endian = 'b';
end

fp=fopen(filename, 'rb', endian);
d = fread(fp, prod(ds), type);
fclose(fp);

d = reshape(d, ds);

There isn’t much magic here, just a simple idea that I use almost daily.

One Response to “Reading raw data in Matlab”

  1. issel says:

    Something that I found is that the endian will change depending on the machine that you use to read/write your data. FSL, for example, uses the computer’s default endian-ness. I have the same function, but my beginning is:

    if (nargin == 3)
    [str,maxsize,endian] = computer;
    end

    That gives the computer’s default “endian”, whether it’s ‘b’ on godzilla or ‘l’ on house.

Leave a Reply

(required)

(required)

Spam Protection by WP-SpamFree

© 2010 Math, Computing and Research Please leave a comment or contact me craig@mri.brechmos.org if you have any questions. Suffusion WordPress theme by Sayontan Sinha