Thread Subject: fprintf Problem

Subject: fprintf Problem

From: Ali Moradi

Date: 3 Oct, 2009 15:40:03

Message: 1 of 7

Hi,

I'm writing a very simple script to do some data analysis off of an experiment and the function 'fprintf' is giving me a LOT of problems. I'm trying to write the calculated data into a text file using the 'fopen' function. This is what I wrote:

>>%Output
>>fid = fopen('NoWinglet100k.txt','wt');
>>fprintf(fid, '%-8.4e %-8.4e\n', rho, rhoe);
>>fclose(fid);

The problem is that when I open the text file, the values for 'rho' and 'rhoe' are not in their own columns. The values for 'rho' are placed in one column for the first 21 rows, then the values of 'rhoe' are placed underneath it. Then in the next column, it finishes printing the rest of the 'rho' values, and underneath it, it places the rest of the 'rhoe' values. Is there any way to make the values come in their own separate columns? Below is the basic output of the text file; the values of "1.1080" belong to the 'rho' column and the rest belong to the 'rhoe' column. If anyone has any suggestions, please let me know. Thank you all in advance.

1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 1.1080e+000
1.1080e+000 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003
4.0906e-003 4.0906e-003

Subject: fprintf Problem

From: Rune Allnor

Date: 3 Oct, 2009 15:56:56

Message: 2 of 7

On 3 Okt, 17:40, "Ali Moradi" <kimus...@gmail.com> wrote:
> Hi,
>
> I'm writing a very simple script to do some data analysis off of an experiment and the function 'fprintf' is giving me a LOT of problems. I'm trying to write the calculated data into a text file using the 'fopen' function. This is what I wrote:
>
> >>%Output
> >>fid = fopen('NoWinglet100k.txt','wt');
> >>fprintf(fid, '%-8.4e        %-8.4e\n', rho, rhoe);
> >>fclose(fid);

Without knowing what the variables look like, it's hard to tell
what went wrong.

Assuming the two variables are stored in vectors of equal length,
this is how I would do things:

fid = fopen('NoWinglet100k.txt','wt');
for n=1:length(rho)
    fprintf(fid, '%-8.4e %-8.4e\n', rho(n), rhoe(n));
end
fclose(fid)

Rune

Subject: fprintf Problem

From: ImageAnalyst

Date: 3 Oct, 2009 15:59:25

Message: 3 of 7

You need to have it in a loop:

% fid = fopen('NoWinglet100k.txt','wt');
rho = 1.108 * ones(41,1)
rhoe = 4.0906e-003 * ones(41,1)
for k = 1 : length(rho)
% For debugging only, print to the command window.
fprintf(1, '%8.4e %8.4e\n', rho(k), rhoe(k));
end
% fclose(fid);

It was doing all of one array and then all of the other. This way
you'll alternate rho and rhoe element by element.

Subject: fprintf Problem

From: Ali Moradi

Date: 3 Oct, 2009 17:06:00

Message: 4 of 7

Oh you guys are freaking awesome! I just got it to work, I didn't realize that it had to be set in a loop. This saves me a ton of time because I won't have to copy and paste every vector into excel (there are 12 vectors and have to be run 12 times!). Thank you again guys, this is awesome.

- Ali

Subject: fprintf Problem

From: Ali Moradi

Date: 3 Oct, 2009 17:08:01

Message: 5 of 7

Oh you guys are freaking awesome! I just got it to work, I didn't realize that it had to be set in a loop. This saves me a ton of time because I won't have to copy and paste every vector into excel (there are 12 vectors and have to be run 12 times!). Thank you again guys, this is awesome.

- Ali

Subject: fprintf Problem

From: TideMan

Date: 3 Oct, 2009 19:40:33

Message: 6 of 7

On Oct 4, 6:08 am, "Ali Moradi" <kimus...@gmail.com> wrote:
> Oh you guys are freaking awesome! I just got it to work, I didn't realize that it had to be set in a loop. This saves me a ton of time because I won't have to copy and paste every vector into excel (there are 12 vectors and have to be run 12 times!). Thank you again guys, this is awesome.
>
> - Ali

Well, actually, you don't need it in a loop:
fprintf(fid, '%-8.4e %-8.4e\n', [rho rhoe]');
will do the trick.
The problem arises because Matlab stores data column-wise, but when
you print you want to print row-wise, so you must transform your
output matrix accordingly.

Subject: fprintf Problem

From: Carlos Adrian Vargas Aguilera

Date: 3 Oct, 2009 20:57:01

Message: 7 of 7

TideMan is right.

You may check out my SAVEASCII function and just text:
>> saveascii([rho rhoe], 'noWinglet100k.txt', '%-8.4e')
it has some others versatilities.

http://www.mathworks.com/matlabcentral/fileexchange/10243

Well, carlos

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
saveascii Carlos Adrian Vargas Aguilera 3 Oct, 2009 16:59:04
fprintf Carlos Adrian Vargas Aguilera 3 Oct, 2009 16:59:04
rssFeed for this Thread

Contact us at files@mathworks.com