Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 如何在文件上指定fprinf数组_Matlab - Fatal编程技术网

Matlab 如何在文件上指定fprinf数组

Matlab 如何在文件上指定fprinf数组,matlab,Matlab,例如,我们有一个数组: x = 0.5920 0.4635 0.6451 0.2118 -0.1206 -0.6036 0.2417 0.4773 0.3029 0.5172 我需要编写什么代码才能以如下方式打印: coords x1 0.5920 y1 0.4635 x2 0.6451 y2 0.2118 x3 -0.1206 y3 -0.6036 x4 0.2417

例如,我们有一个数组:

x =

    0.5920    0.4635
    0.6451    0.2118
   -0.1206   -0.6036
    0.2417    0.4773
    0.3029    0.5172
我需要编写什么代码才能以如下方式打印:

    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
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
fprintf(fileID,'%.4f %.4f\n', coord.'); % .' tranposes the matrix
我试过这个:

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);
但我得到的结果如下:

    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
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
fprintf(fileID,'%.4f %.4f\n', coord.'); % .' tranposes the matrix

fprintf
函数首先以列的方式读取输入变量,并将每个值发送到字符串中的适当位置。因此,在您的代码中,即使您在代码中为每个
%.4f
指定了两个不同的向量,Matlab也会忽略该顺序。它将
coord(:,1)
的第一个值放在第一个
%.4f
中,将
coord(:,1)
的第二个值放在第二个
%.4f
中。然后它打破了界限。然后它再次从
坐标(:,1)
中提取第三个值,并将其放入第一个
%.4f
中,依此类推。当第一个向量的所有值都用尽时,它仅从
坐标(:,2)
中拾取值

最简单的解决方法是将
坐标
矩阵转置,然后将其输入Matlab,如下所示:

    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
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
fprintf(fileID,'%.4f %.4f\n', coord.'); % .' tranposes the matrix
编辑:

为了得到格式为
x10.5920 y1 0.4635
,我们可以使用Matlab遵循的列优先顺序来访问变量

% 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);
为什么这样做有效

如果查看
tempCoord
,您将看到它的每一列都具有字符串说明符所需的格式,即x的索引、x的值、y的索引和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
现在,每列都成为打印文件的每一行,您将获得以下输出:

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

你能解释一下为什么
coord.
解决了这个问题吗?我知道它解决了这个问题,但是
做了什么?另外,我想实现这个
x10.5920 y1 0.4635
。我试过这个
fprintf(文件ID,'x%d%.4fy%d%.4f\n',I,(coord'),但这是我在文件
x1 1.9914 y3.345587e-01 0.7785 x9.714147e-01 shape 2中得到的输出:
只是转置矩阵。我将更新答案,以便您可以打印
x1 0.5920 y1 0.4635
是操作员的简写符号。