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:
writing matrix to .dat file

Subject: writing matrix to .dat file

From: Stefan

Date: 28 Nov, 2011 16:41:08

Message: 1 of 4

L.S.

I'm trying to write a matrix to a .dat file. But I'm not succeeding. My code:

fname = ' *.dat'

fout =

fid = fopen (fname, 'w');
fwrite(fid, fout, 'uint8');
fclose(fid);

with fout:

fout = [ 1 2 61.10 4.23
           2 3 120.44 2.33
           3 4 117.98 2.38];

I also tried:

save -ascii fname fout

thx


Stefan

Subject: writing matrix to .dat file

From: Parag Chandakkar

Date: 28 Nov, 2011 17:11:15

Message: 2 of 4

On Nov 28, 9:41 am, "Stefan " <s.g.h.hei...@student.tue.nl> wrote:
> L.S.
>
> I'm trying to write a matrix to a .dat file. But I'm not succeeding. My code:
>
> fname = ' *.dat'
>
> fout =
>
> fid = fopen (fname, 'w');
> fwrite(fid, fout, 'uint8');
> fclose(fid);
>
> with fout:
>
> fout = [ 1 2   61.10    4.23
>            2 3   120.44  2.33
>            3 4   117.98  2.38];
>
> I also tried:
>
> save -ascii fname fout
>
> thx
>
> Stefan

use textwrite or dlmwrite with delimiter as space in your case

Subject: writing matrix to .dat file

From: Barry Williams

Date: 28 Nov, 2011 18:46:09

Message: 3 of 4

Parag Chandakkar <pchandak@asu.edu> wrote in message <c9b0ee34-5720-4ec5-ac7e-a5856d4594a3@j10g2000vbe.googlegroups.com>...
> On Nov 28, 9:41 am, "Stefan " <s.g.h.hei...@student.tue.nl> wrote:
> > L.S.
> >
> > I'm trying to write a matrix to a .dat file. But I'm not succeeding. My code:
> >
> > fname = ' *.dat'
> >
> > fout =
> >
> > fid = fopen (fname, 'w');
> > fwrite(fid, fout, 'uint8');
> > fclose(fid);
> >
> > with fout:
> >
> > fout = [ 1 2   61.10    4.23
> >            2 3   120.44  2.33
> >            3 4   117.98  2.38];
> >
> > I also tried:
> >
> > save -ascii fname fout
> >
> > thx
> >
> > Stefan
>
> use textwrite or dlmwrite with delimiter as space in your case

Stefan, I assume you are replacing the wildcard in '*.dat' with valid file characters.
I believe you may be new to ML and are trying to learn how to use fwrite. There can be advantages to using a binary file format as opposed to dlmwrite or csvwrite (although not readily apparent with smaller matrices).
Because your matrix contains decimal values, you will need to use a precision that encompasses these, either float32 or float 64.
Example where fout is a 50 X 50 matrix of double precision values:

fid=fopen('test_bin.dat','w+');
fwrite(fid,fout,'float32');
fclose(fid);
Size of file on disk = 12288 bytes

dlmwrite('test_dlm.csv',fout,'precision',8)
Size of file on disk = 24576 bytes

Barry

Subject: writing matrix to .dat file

From: TideMan

Date: 28 Nov, 2011 22:20:08

Message: 4 of 4

On Nov 29, 5:41 am, "Stefan " <s.g.h.hei...@student.tue.nl> wrote:
> L.S.
>
> I'm trying to write a matrix to a .dat file. But I'm not succeeding. My code:
>
> fname = ' *.dat'
>
> fout =
>
> fid = fopen (fname, 'w');
> fwrite(fid, fout, 'uint8');
> fclose(fid);
>
> with fout:
>
> fout = [ 1 2   61.10    4.23
>            2 3   120.44  2.33
>            3 4   117.98  2.38];
>
> I also tried:
>
> save -ascii fname fout
>
> thx
>
> Stefan

So, you're trying to write a file with ASCII data.
Why not use the function that is designed to do this, namely fprintf,
rather than fwrite which is for binary data:
fid=fopen(.....
[nr,nc]=size(fout)
% Define format
fmt=repmat('%f\t',1,nc);
fmt(end)='n'); % Put a LF at the end
fprintf(fid,fmt,fout'); % Note: you need to transpose the mx
fclose(fid)

Tags for 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