Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

save number as 999999 instead of 9.9999900e+005

Status
Not open for further replies.

kerstinm

Industrial
Apr 6, 2006
7
Hello,

I am trying to export (save, write, ...?) a vector from Matlab, so I can open it in an editor (PFE). (I am using Matlab to manipulate data). Saving the file works fine but when I open the file in an editor the figures are shown as 9.9999900e+005 instead of 999999.

Is there any command that tells Matlab to save (write, ...) the figures as 999999 (instead of 9.9999900e+005)?

So far I've tried
- save file.txt file -ascii and
- dlmwrite ('file.txt', file, '9')
but there might be another command?

Thanks for your help!
Kerstin
 
Replies continue below

Recommended for you

Try using fprintf() to write your data. Use the %f format specifer.
 
%.0d may be better if you do not want anything after the decimal point.

M

--
Dr Michael F Platten
 
Thanks for your quick replies!

Unfortunately I am new to Matlab and don't understand the fprintf command very well... I used fprintf ('%7.4f\n', filename) which rewrote my vector in the format I am looking for, but it seems like I didn't save it at the same time. Can I use fprintf to set the format and save it at the same time (i.e. have a file that I can open in an editor)? If so, how would the command look like?

At the moment I am only working with a vector, but later I'll have to export matrices too. Can I use fprintf in this case too?

Thanks again for your help! I really appreciate it.
 
you need to use the "fopen" command to create the file first

A = %vector to write to file%

filevar = fopen('testfile.txt','w')
fprintf(filevar, '%7.4f\n', A)
fclose(filever)

M

--
Dr Michael F Platten
 
It seems as soon as one question is answered another one arises...

I am having a matrix with three columns (and lots of rows) and would like to create a text file in a special format.

Using fprintf has solved my formatting problem and it is now like I want it. But... in my txt file the colums are written one below the other instead of being next to each other,
i.e. it should be 1 2 3 but it is
1
2
3

I used the following command:

A = matrixVariableInMatlabWithThreeColumns;
fid = fopen ('matrix.txt','w');
fprintf(fid,'%7.0d %7.0d %13.6f\n',A);
fclose(fid)

Could anyone tell me what I need to change in my commands to ensure that the colums are written next to each other (i.e. as a matrix)? I've tried so many different commands but non seems to be the right one.

Thanks a lot!!!
 
Have you tried transposing the matrix A?

M

--
Dr Michael F Platten
 
Hi!

No, I haven't. But thinking about it, I realised I might have explained my problem incorrectly.

My matrix looks actually like this:

531 531 1.00
531 532 5.000
531 533 17.000
531 534 2.000

while the txt file looks like this:
531
531
531
531
531
532
533
534
1.000
5.000
17.000
2.000

It seems I need to define somewhere that the columns should be printed next to each other (in a matrix format?), but I can't find a suitable command.
I am also not quite sure if I need to tell Matlab that the variable is a matrix and that it should thus write a matrix. Or if it knows that is given a matrix (I suppose so) but that I need to tell it specifically that it should write a matrix too?

Thanks for your help!
 
Works for me:

>> a=magic(3);
>> fid=fopen('out','w');
>> fprintf(fid,'%.0f %.0f %.0f\n',a);
>> fclose(fid);
>> !more out
8 3 4
1 5 9
6 7 2

 
Thanks again! I finally understood transpose :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor