Info

This question is closed. Reopen it to edit or answer.

How to make sure that matlab answer in command window is copied to text file in two distinct columns(in exactly the same way as a mirror image of the command window)?

1 view (last 30 days)
A script is made to produce all permutations of vector x with vector y. so each element of x is assigned to each element of y. %------- function rowpermutation_3 = rowpermutation3(x,y) bla = 4 for k = 1:1e3 [ix, iy] = find(true(size(y, 1), size(x, 1))); rowpermutation_3 = [x(ix(:), :), y(iy(:), :)]; rowpermutation_3 = [kron(x, ones(size(y,1),1)), repmat(y, [size(x, 1), 1])]; rowpermutation_3column1 = rowpermutation_3(:,1); rowpermutation_3column2 = rowpermutation_3(:,2); end fid = fopen('fprintfrowpermutation3.txt','wt'); fprintf(fid, '%d %f\n',[rowpermutation_3column1,rowpermutation_3column2]); fclose(fid) end %--------- The matlab commandow does produce the desired outcome,which is like this: 1 21 1 22 1 23 1 24 1 25 2 21 2 22 2 23 2 24 2 25 3 21 3 22 3 23 3 24 3 25 4 21 4 22 4 23 4 24 4 25 5 21 5 22 5 23 5 24 5 25 however my text file produces something different: 1 1.000000 1 1.000000 1 2.000000 2 2.000000 2 2.000000 3 3.000000 3 3.000000 3 4.000000 4 4.000000 4 4.000000 5 5.000000 5 5.000000 5 21.000000 22 23.000000 24 25.000000 21 22.000000 23 24.000000 25 21.000000 22 23.000000 24 25.000000 21 22.000000 23 24.000000 25 21.000000 22 23.000000 24 25.000000

Answers (1)

Geoff Hayes
Geoff Hayes on 1 Sep 2014
Antoine - running your above code as (for example)
rowpermutation3(1:5,22:25)
does not seem to produce the desired output, so you may want to revisit the above code. It is unclear (to me) why you wish to iterate over k 1000 times without referencing k in any way. Also, data is overwritten from one iteration to the next, so that doesn't seem quite correct especially as the fprintf statement is outside of the for loop so only one line will be written to file. Perhaps this code is not the same as what produced the above output?
As for your question about why the text file output is not identical to the output in the Command Window, look at your line of code that writes the data to file
fprintf(fid, '%d %f\n',[rowpermutation_3column1,rowpermutation_3column2]);
Note how the first value is written as an integer using the %d and the second value is written in fixed-point notation using the %f. That is why you will see something like
24 25.000000
in your text file and not the
24 25
that appears in the Command Window. Presumably all of your inputs are integers, so just change the fprintf to
fprintf(fid, '%d %d\n',[rowpermutation_3column1,rowpermutation_3column2]);
This will fix only part of the problem. As for text in the Command Window being written as
1 21
1 22
1 23
1 24
1 25
2 21
etc.
5 24
5 25
but appearing in a different order in the text file as
1 1.000000
1 1.000000
1 2.000000
2 2.000000
2 2.000000
3 3.000000
etc.
22 23.000000
24 25.000000
that is because fprintf writes matrix data (to file) in column order. See the examples from the given link. To fix this, you just have to transpose your matrix before writing it to file. Your fprintf statement then becomes
fprintf(fid, '%d %d\n',A');
where
A = [rowpermutation_3column1,rowpermutation_3column2];
Try the above and see what happens!

Community Treasure Hunt

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

Start Hunting!