Thread Subject: Export data to CSV and Format of numbers

Subject: Export data to CSV and Format of numbers

From: Learner

Date: 30 Aug, 2008 03:33:07

Message: 1 of 10

,gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:487929

Hi ,

I want to export a matrix to csv, but found the format of the numbers
are all "short" in the CSV file. I used "format long" in my codes, and
the number format of data is correct when I open it in Matlab.

How can I get the exported data in the right format?

Thank you!

Learner

Subject: Export data to CSV and Format of numbers

From: dpb

Date: 30 Aug, 2008 05:07:23

Message: 2 of 10

Learner wrote:
> Hi ,
>
> I want to export a matrix to csv, but found the format of the numbers
> are all "short" in the CSV file. I used "format long" in my codes, and
> the number format of data is correct when I open it in Matlab.
>
> How can I get the exported data in the right format?

Looking at the code for csvwrite(), it call dlmwrite() which uses num2str().

The help files for num2str() document that it returns about four digits
of precision.

Looks like you'll have to use sprintf() and roll your own unless newer
versions of Matlab have additional flexibility already...

--

Subject: Export data to CSV and Format of numbers

From: dpb

Date: 30 Aug, 2008 14:10:47

Message: 3 of 10

dpb wrote:
...
> Looks like you'll have to use sprintf() and roll your own unless newer
> versions of Matlab have additional flexibility already...

But num2str() has the optional argument N to specify maximum number of
digits so while didn't look explicitly, appears one could relatively
quickly make a custom csvwrite() function to either hardcode the number
of digits or accept another optional argument to pass the number of
digits into it for num2str() to use...

--

Subject: Export data to CSV and Format of numbers

From: Walter Roberson

Date: 30 Aug, 2008 14:15:03

Message: 4 of 10

Learner wrote:

> I want to export a matrix to csv, but found the format of the numbers
> are all "short" in the CSV file.

Use the Precision option to dlmwrite()

--
Q = quotation(rand);
if isempty(Q); error('Quotation server filesystem problems')
else sprintf('%s',Q), end

Subject: Export data to CSV and Format of numbers

From: dpb

Date: 30 Aug, 2008 14:32:47

Message: 5 of 10

Walter Roberson wrote:
> Learner wrote:
>
>> I want to export a matrix to csv, but found the format of the numbers
>> are all "short" in the CSV file.
>
> Use the Precision option to dlmwrite()

That would be one of those enhancements "already available" I mentioned,
but it postdates R12... :)

--

Subject: Export data to CSV and Format of numbers

From: Donn Shull

Date: 30 Aug, 2008 16:31:01

Message: 6 of 10

Learner <cnfengshuang@gmail.com> wrote in message <4374cd39-
d419-481d-a3b9-b770c620f6c9@l64g2000hse.googlegroups.com>...
> ,gzip(gfe),gzip(gfe)
> Xref: news.mathworks.com comp.soft-sys.matlab:487929
>
> Hi ,
>
> I want to export a matrix to csv, but found the format of
the numbers
> are all "short" in the CSV file. I used "format long" in
my codes, and
> the number format of data is correct when I open it in
Matlab.
>
> How can I get the exported data in the right format?
>
> Thank you!
>
> Learner

Use dlmwrite which allows you to specify the precision.
csvwrite calls dlmwrite.

Donn

Subject: Export data to CSV and Format of numbers

From: Tim Farajian

Date: 31 Aug, 2008 00:39:01

Message: 7 of 10

Learner <cnfengshuang@gmail.com> wrote in message <4374cd39-
d419-481d-a3b9-b770c620f6c9@l64g2000hse.googlegroups.com>...
> ,gzip(gfe),gzip(gfe)
> Xref: news.mathworks.com comp.soft-sys.matlab:487929
>
> Hi ,
>
> I want to export a matrix to csv, but found the format of
the numbers
> are all "short" in the CSV file. I used "format long" in
my codes, and
> the number format of data is correct when I open it in
Matlab.
>
> How can I get the exported data in the right format?
>
> Thank you!
>
> Learner

The "format long" command only affects how numbers are
displayed on the screen. It doesn't affect how numbers are
written into files.

If you want to write data into a CSV file with a specified
precision, use the DLMWRITE function. Specify the
delimiter to be ',' and specify the precision to what you
want. For example:

A = rand(3,3);
dlmwrite('test.csv',A,...
'delimiter',',','precision','%0.16f')

For more info type

doc dlmwrite

Subject: Export data to CSV and Format of numbers

From: Phil Goddard

Date: 31 Aug, 2008 17:29:02

Message: 8 of 10


Use dlmwrite instead of csvwrite as it allows a precision
to be set.

There are a couple of examples of doing this in the help
>> doc dlmwrite

Phil.

Subject: Export data to CSV and Format of numbers

From: Rodney Thomson

Date: 1 Sep, 2008 04:37:01

Message: 9 of 10

Learner <cnfengshuang@gmail.com> wrote in message
<4374cd39-d419-481d-a3b9-b770c620f6c9@l64g2000hse.googlegroups.com>...
> ,gzip(gfe),gzip(gfe)
> Xref: news.mathworks.com comp.soft-sys.matlab:487929
>
> Hi ,
>
> I want to export a matrix to csv, but found the format of
the numbers
> are all "short" in the CSV file. I used "format long" in
my codes, and
> the number format of data is correct when I open it in Matlab.
>
> How can I get the exported data in the right format?
>
> Thank you!
>
> Learner

You could use 'dlmwrite' instead of csvwrite and supply a
'precision' flag:

m = rand(10);
dlmwrite('c:\tmp.csv', m, 'delimiter', ',', 'newline', 'pc',
'precision', '%.12f'); % 12 decimal places

Rod

--
http://iheartmatlab.blogspot.com

Subject: Export data to CSV and Format of numbers

From: Felix Hebeler

Date: 1 Sep, 2008 11:57:02

Message: 10 of 10

Learner <cnfengshuang@gmail.com> wrote in message
<4374cd39-d419-481d-a3b9-b770c620f6c9@l64g2000hse.googlegroups.com>...
> ,gzip(gfe),gzip(gfe)
> Xref: news.mathworks.com comp.soft-sys.matlab:487929
>
> Hi ,
>
> I want to export a matrix to csv, but found the format of
the numbers
> are all "short" in the CSV file. I used "format long" in
my codes, and
> the number format of data is correct when I open it in Matlab.
>
> How can I get the exported data in the right format?
>
> Thank you!
>
> Learner

If you use dlmwrite, you can specify the precision
e.g.
R=randn(10,10);

dlmwrite('myfile.txt', R, 'delimiter',
'\t','precision','%10.8f');

doc dlmwrite gives you more information.

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
precision Felix Hebeler 1 Sep, 2008 17:13:25
dlmwrite Felix Hebeler 1 Sep, 2008 17:13:25
csvwrite Phil Goddard 1 Sep, 2008 17:12:18
dlmwrite Phil Goddard 1 Sep, 2008 17:12:18
precision Phil Goddard 1 Sep, 2008 17:12:18
jepg **<--DHAMU-->** 30 Aug, 2008 03:47:25
rssFeed for this Thread

Contact us at files@mathworks.com