skippy
skippy

Reputation: 67

How to specifically fprintf an array on a file

Lets say for example we have this array:

x =

    0.5920    0.4635
    0.6451    0.2118
   -0.1206   -0.6036
    0.2417    0.4773
    0.3029    0.5172

What code would I need to write in order to print in such a way that it looks like this:

    coords
    x1 0.5920    y1 0.4635
    x2 0.6451    y2 0.2118
    x3 -0.1206   y3 -0.6036
    x4 0.2417    y4 0.4773
    x5 0.3029    y5 0.5172

I've tried this:

x = gallery('uniformdata',[1,10],0);
y = gallery('uniformdata',[1,10],1);


[v,c] = voronoin([x(:) y(:)]); %returns an array V with vertices and a cell array C with a matrix for each cell of the diagram. 
c
for k = 1 : numel(c)
     c{k} = c{k}(c{k} ~= 1)
end

fileID = fopen('cords.txt' , 'w');
    for i=1:10
        coord = v(c{i},:);
        fprintf(fileID,'shape %d:\nx \t y\n', i);
        fprintf(fileID,'%.4f %.4f\n', coord(:,1), coord(:,2));

    end
    fclose(fileID);

but im getting an output like this:

shape 10:
x    y
0.5920 0.6451 %notice how the .6451 is on the right side when it should be on the bottom
-0.1206 0.2417
0.3029 0.4635
0.2118 -0.6036
0.4773 0.5172

Upvotes: 1

Views: 889

Answers (1)

ammportal
ammportal

Reputation: 991

The fprintf function reads the input variables in a column first manner and sends each value to its appropriate place in the string. So, in your code what happens is that even when you specify two different vectors per %.4f in your code, Matlab ignores that ordering. It puts the first value of coord(:, 1) in the first %.4f and the second value of coord(:, 1) in the second %.4f. Then it breaks the line. Then it again picks up the third value from coord(:, 1) and puts it in the first %.4f and so on. It only picks values from coord(:, 2) when all values of the first vector are exhausted.

The simplest fix is to transpose the coord matrix and then input it to Matlab like this:

fprintf(fileID,'%.4f %.4f\n', coord.'); % .' tranposes the matrix

Edit:

To get the format as x1 0.5920 y1 0.4635, we can make use of the column first ordering that Matlab follows to access a variable

% First we make a new matrix that has each of the required elements for the desired format
% The index of x, the value of x, the index of y and the value of y
tempCoord = [1:size(coord, 1); coord(:, 1).'; 1:size(coord, 1); coord(:, 2).'];
% Now we change the string specification for fprintf
fprintf(fileID,'x%d %.4f y%d %.4f\n', tempCoord);

Why does this work?

If you look at tempCoord, you will see that each of its columns has the format needed for the string specifier, i.e., the index of x, the value of x, the index of y and the value of y

tempCoord =
   1.000000000000000   2.000000000000000   3.000000000000000   4.000000000000000   5.000000000000000
   0.592000000000000   0.645100000000000  -0.120600000000000   0.241700000000000   0.302900000000000
   1.000000000000000   2.000000000000000   3.000000000000000   4.000000000000000   5.000000000000000
   0.463500000000000   0.211800000000000  -0.603600000000000   0.477300000000000   0.517200000000000

Now each column becomes each row of the printed file and you get the following output:

x1 0.5920 y1 0.4635
x2 0.6451 y2 0.2118
x3 -0.1206 y3 -0.6036
x4 0.2417 y4 0.4773
x5 0.3029 y5 0.5172

Upvotes: 3

Related Questions