Thread Subject: Save a vector as a binary file

Subject: Save a vector as a binary file

From: Ida Haggstrom

Date: 8 Oct, 2009 12:49:02

Message: 1 of 3

Hi!

I'm trying to save a vector in Matlab as a binary file. Here's what I've done:
I already have a 32bit binary 3D image (128x128x30 pixels) that I open in Matlab using:

A = fread(fopen(filename),inf,'float32');
size(A) = 491520 1

I then reshape and pick out only a smaller volume of the image:

B = reshape(A,[128 128 30]); B=B(100:110,100:110,20:30);
size(B) = 11 11 11
B=B(:);
size(B) = 1331 1

I now want to save this new, smaller image (actually vector, B) in the same binary format as the original "filename" format. That is, I want to save the file in "filename2", and be able to open it again in the same way as I opened "filename":

C = fread(fopen(filename2),inf,'float32');
size(C) = 1331 1

I've tried saving it as a mat-file, but when I open it again (fopen and fread) I get a much shorter vector, so obviously something is lost. Any other suggestions? Thanks a lot!
Ida

Subject: Save a vector as a binary file

From: Rune Allnor

Date: 8 Oct, 2009 12:54:47

Message: 2 of 3

On 8 Okt, 14:49, "Ida Haggstrom" <ida_haggst...@yahoo.se> wrote:
> Hi!
>
> I'm trying to save a vector in Matlab as a binary file. Here's what I've done:
> I already have a 32bit binary 3D image (128x128x30 pixels) that I open in Matlab using:
>
> A = fread(fopen(filename),inf,'float32');
> size(A) = 491520      1
>
> I then reshape and pick out only a smaller volume of the image:
>
> B = reshape(A,[128 128 30]); B=B(100:110,100:110,20:30);
> size(B) = 11      11     11
> B=B(:);
> size(B) = 1331     1
>
> I now want to save this new, smaller image (actually vector, B) in the same binary format as the original "filename" format. That is, I want to save the file in "filename2", and be able to open it again in the same way as I opened "filename":
>
> C = fread(fopen(filename2),inf,'float32');
> size(C) = 1331    1
>
> I've tried saving it as a mat-file, but when I open it again (fopen and fread) I get a much shorter vector, so obviously something is lost. Any other suggestions? Thanks a lot!
> Ida

I assume you want to know what statements save the
data to that binary format? If so, something like

fout=fopen(filename2,'wb');
fwrite(fout,B,'float32');
fclose(fout);

should get you close.

Rune

Subject: Save a vector as a binary file

From: Ida Haggstrom

Date: 8 Oct, 2009 13:29:03

Message: 3 of 3

Hello Rune
and thanks for your reply! This was exactly what I was looking for, thank you!
Ida

Rune Allnor <allnor@tele.ntnu.no> wrote in message <ab484993-1e7b-
> I assume you want to know what statements save the
> data to that binary format? If so, something like
>
> fout=fopen(filename2,'wb');
> fwrite(fout,B,'float32');
> fclose(fout);
>
> should get you close.
>
> Rune

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
binary save ope... Ida Haggstrom 8 Oct, 2009 08:54:03
rssFeed for this Thread

Contact us at files@mathworks.com