From: Mazyar Amin on
Hi,

I am reading a file (test.dat) which has 2 columns and I want to print it in new file: merge.dat (of course it is more complicated than that but I have simplified it for better description). I use textscan because my file has some text too, but I get rid of them first.
Assume that the test.dat contains some numeric data like:

1 5
2 6
3 7
4 8

%=================================
fid1=fopen('test.dat','rt');
fid3=fopen('merge.dat','wt');

cdata1=textscan(fid1,'%7.5f %7.5f ','CollectOutput',1);
M=cdata1{1};

for i=1:4 % number of rows in test.dat
fprintf(fid3, '%7.5f %7.5f\n', M(i,1), M(i,2));
end
%=================================

This program just works fine; but I want to know why when I eliminated the for-loop (as following), it prints the 1st column first and then the 2nd column in continue; which is not desirable.

fprintf(fid3, '%7.5f %7.5f\n', M( : ,1), M( : ,2));

output:

1 2
3 4
5 6
7 8

Thanks,
Mazyar
From: dpb on
Mazyar Amin wrote:
> Hi,
>
> I am reading a file (test.dat) which has 2 columns and I want to print
> it in new file: merge.dat (of course it is more complicated than that
> but I have simplified it for better description). I use textscan because
> my file has some text too, but I get rid of them first.
> Assume that the test.dat contains some numeric data like:
>
> 1 5
> 2 6
> 3 7
> 4 8
>
> %=================================
> fid1=fopen('test.dat','rt');
> fid3=fopen('merge.dat','wt');
>
> cdata1=textscan(fid1,'%7.5f %7.5f ','CollectOutput',1);
> M=cdata1{1};
>
> for i=1:4 % number of rows in test.dat
> fprintf(fid3, '%7.5f %7.5f\n', M(i,1), M(i,2));
> end
> %=================================
>
> This program just works fine; but I want to know why when I eliminated
> the for-loop (as following), it prints the 1st column first and then the
> 2nd column in continue; which is not desirable.
>
> fprintf(fid3, '%7.5f %7.5f\n', M( : ,1), M( : ,2));
>
> output:
>
> 1 2
> 3 4
> 5 6
> 7 8
>
> Thanks,
> Mazyar
From: Jan Simon on
Dear Mazyar!

> Assume that the test.dat contains some numeric data like:
> 1 5
> 2 6
> 3 7
> 4 8

> fprintf(fid3, '%7.5f %7.5f\n', M( : ,1), M( : ,2));

What about:
fprintf(fid3, '%7.5f %7.5f\n', transpose(M));

Kind regards, Jan
From: dpb on
Mazyar Amin wrote:
....
> ...[why the] following ... prints the 1st column first and then the
> 2nd column ...
>
> fprintf(fid3, '%7.5f %7.5f\n', M( : ,1), M( : ,2));
>
....

Because you pass the first column _then_ the second column of M to
fprintf(). Arguments are processed left to right by fprintf() (a la C
syntax from whence it cameth).

--
From: dpb on
dpb wrote:
> Mazyar Amin wrote:
> ...
>> ...[why the] following ... prints the 1st column first and then the
>> 2nd column ...
>>
>> fprintf(fid3, '%7.5f %7.5f\n', M( : ,1), M( : ,2));
>>
> ...
>
> Because you pass the first column _then_ the second column of M to
> fprintf(). Arguments are processed left to right by fprintf() (a la C
> syntax from whence it cameth).
>
> --

Dang! I've got the early return syndrome today bad...first a completely
empty now a before "the rest of the story"... :(

What you're looking for is

>> M=reshape([1:8],4,2)
M =
1 5
2 6
3 7
4 8
>> fprintf('%f %f\n', M')
1.000000 5.000000
2.000000 6.000000
3.000000 7.000000
4.000000 8.000000
>>

--