Chnage number display format

Hi,
The numbers in the first column of my output writing to a text file shows like 3e+006.
But I want to display like 3000001.
How can I change that in MATLAB?
Thanks.

Réponses (1)

Sean de Wolski
Sean de Wolski le 23 Mai 2014

0 votes

How are you writing it right now? If you are using fprintf you can change the format spec. For example:
fprintf(1,'%10.0f\n',randi(10,[5, 1])*2347348)

4 commentaires

Damith
Damith le 23 Mai 2014
No. I could not figure it out.
I have attached a sample file. I just want to replace the column 4 of the sample text file by values of matrix D. (Only column 4 in sample text file to be replaced everything else remais same). Matrix D has same number of rows.
Can you please help me with that?
Thanks.
Sean de Wolski
Sean de Wolski le 23 Mai 2014
Modifié(e) : Sean de Wolski le 23 Mai 2014
I would read in the entire file with csvread or dlmread, and then write it back out with dlmwrite which offers precision. IF you need finer control over the formatspec, then fprintf would work here as well.
Damith
Damith le 23 Mai 2014
I sort of figure it out. But I am still struggling to maintain the spacing between 3rd columns and 4th column. There shoud be 3 spacing between col 3 and 4.
sample=dlmread('sample.txt',' ','A2..D10');
dlmwrite('test.txt',sample,'delimiter',' ','precision', 10)
How can I modify the above code to achieve that?
Thanks.
Damith
Damith le 24 Mai 2014
Hi,
I was able to develop the code below. But in the command window I can see the output the way I want to produce. But the test.txt file is blank.
Command window output:
3000001 -99.9 -99.9 0.0
3000002 -99.9 -99.9 0.0
3000003 -99.9 -99.9 0.0
3000004 -99.9 -99.9 0.0
3000005 -99.9 -99.9 0.0
3000006 -99.9 -99.9 0.0
3000007 -99.9 -99.9 0.0
3000008 -99.9 -99.9 0.0
3000009 -99.9 -99.9 0.8
3000010 -99.9 -99.9 0.0
I dont understand what I am doing wrong here and why the test.txt file is bank. I want the command window output to be written in the text file.
sample=dlmread('sample.txt',' ','A2..C11');
Data=[sample(1:10,:) Gist1(1:10,5)];
dlmwrite('test.txt',Data,'delimiter',' ','precision', 10)
fid = fopen('test.txt','wt');
for row = 1 : size(Data, 1);
fprintf('%d %.1f %.1f %3.1f\n', Data(row, :));
end
fclose(fid);
Can somebody help me to figure out what I am missing here.
Many thanks in advance.

Connectez-vous pour commenter.

Question posée :

le 23 Mai 2014

Commenté :

le 24 Mai 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by