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中将数据输出到文件中?_Matlab_File Io - Fatal编程技术网

如何在matlab中将数据输出到文件中?

如何在matlab中将数据输出到文件中?,matlab,file-io,Matlab,File Io,我读过几篇关于如何将数据输出到文件的文章,但它似乎不适合我。我的output.txt文件与我的matlab文件位于同一文件夹中,它只是不向该文件输出任何数据 怎么了?我想在单击图像时输出坐标,这样所有输出逻辑都在侦听器中 function main clc; close all; % set the range of the axes % The image will be stretched to this. min_x = 0; max_x = 1;

我读过几篇关于如何将数据输出到文件的文章,但它似乎不适合我。我的output.txt文件与我的matlab文件位于同一文件夹中,它只是不向该文件输出任何数据

怎么了?我想在单击图像时输出坐标,这样所有输出逻辑都在侦听器中

function main 
    clc; close all;
    % set the range of the axes
    % The image will be stretched to this.
    min_x = 0;
    max_x = 1;
    min_y = 0;
    max_y = 1;

    % replace with an image of your choice
    img = imread('ryklys.jpg');

    imagesc([min_x max_x], [min_y max_y], flipud(img));
    hold on;
    colormap(gray);

    set(gca,'ydir','normal')

    cursor_handle = plot(0,0,'r+ ','visible','off')

    % now attach the function to the axes
    set(gca,'ButtonDownFcn', @mouseclick_callback)

    % and we also have to attach the function to the children, in this
    % case that is the line in the axes.
    set(get(gca,'Children'),'ButtonDownFcn', @mouseclick_callback)



   function mouseclick_callback(gcbo,eventdata)
      % the arguments are not important here, they are simply required for
      % a callback function. we don't even use them in the function,
      % but Matlab will provide them to our function, we we have to
      % include them.
      %
      % first we get the point that was clicked on
      cP = get(gca,'Currentpoint');
      x = cP(1,1);
      y = cP(1,2);
      % Now we find out which mouse button was clicked, and whether a
      % keyboard modifier was used, e.g. shift or ctrl
      switch get(gcf,'SelectionType')
          case 'normal' % Click left mouse button.
              s = sprintf('left: (%1.4g, %1.4g) level = %1.4g',x,y, x.*exp(-x.^2-y.^2));
          case 'alt'    % Control - click left mouse button or click right mouse button.
              s = sprintf('right: (%1.4g, %1.4g level = %1.4g)',x,y, x.*exp(-x.^2-y.^2));
          case 'extend' % Shift - click left mouse button or click both left and right mouse buttons.
              s = sprintf('2-click: (%1.4g, %1.4g level = %1.4g)',x,y, x.*exp(-x.^2-y.^2));
          case 'open'   % Double-click any mouse button.
              s = sprintf('double click: (%1.4g, %1.4g) level = %1.4g',x,y, x.*exp(-x.^2-y.^2));
      end
      % get and set title handle
      thandle = get(gca,'Title');
      set(thandle,'String',s);
      % finally change the position of our red plus, and make it
      % visible.
      set(cursor_handle,'Xdata',x,'Ydata',y,'visible','on')

      % append file with coordinates
      plot(x,y,'r.','MarkerSize',20);

      % Output to file 
        fid=fopen('output.txt','w');
        fprintf(fid, '%4.2f %4.2f \n', x, y);
        fclose(fid);
  end
end

本项目是关于如何在图像鼠标点击时获取坐标并将点坐标输出到文件。

首次启动此文件时,请确保选择“更改文件夹”而不是“将文件添加到路径”。当我选择“更改文件夹”时,Matlab能够在“output.txt”文件中写入数据。 另外,正如Dev-iL所建议的,我添加了append模式,最后的代码如下所示

function main 
    clc; close all;
    % Total points counter 
    counter = 0;

    % set the range of the axes
    % The image will be stretched to this.
    min_x = 0;
    max_x = 1;
    min_y = 0;
    max_y = 1;

    % replace with an image of your choice
    img = imread('ryklys.jpg');

    imagesc([min_x max_x], [min_y max_y], flipud(img));
    hold on;
    colormap(gray);

    set(gca,'ydir','normal')

    cursor_handle = plot(0,0,'r+ ','visible','off')

    % now attach the function to the axes
    set(gca,'ButtonDownFcn', @mouseclick_callback)

    % and we also have to attach the function to the children, in this
    % case that is the line in the axes.
    set(get(gca,'Children'),'ButtonDownFcn', @mouseclick_callback)

%     % Output to file 
    outputFile = 'output.txt';
    fid=fopen(outputFile,'wt');
    fclose(fid);

   function mouseclick_callback(gcbo,eventdata)
      % the arguments are not important here, they are simply required for
      % a callback function. we don't even use them in the function,
      % but Matlab will provide them to our function, we we have to
      % include them.
      %
      % first we get the point that was clicked on
      cP = get(gca,'Currentpoint');
      x = cP(1,1);
      y = cP(1,2);
      % Now we find out which mouse button was clicked, and whether a
      % keyboard modifier was used, e.g. shift or ctrl
      switch get(gcf,'SelectionType')
          case 'normal' % Click left mouse button.
              s = sprintf('left: (%1.4g, %1.4g) level = %1.4g',x,y, x.*exp(-x.^2-y.^2));
          case 'alt'    % Control - click left mouse button or click right mouse button.
              s = sprintf('right: (%1.4g, %1.4g level = %1.4g)',x,y, x.*exp(-x.^2-y.^2));
          case 'extend' % Shift - click left mouse button or click both left and right mouse buttons.
              s = sprintf('2-click: (%1.4g, %1.4g level = %1.4g)',x,y, x.*exp(-x.^2-y.^2));
          case 'open'   % Double-click any mouse button.
              s = sprintf('double click: (%1.4g, %1.4g) level = %1.4g',x,y, x.*exp(-x.^2-y.^2));
      end
      % get and set title handle
      thandle = get(gca,'Title');
      set(thandle,'String',s);
      % finally change the position of our red plus, and make it
      % visible.
      set(cursor_handle,'Xdata',x,'Ydata',y,'visible','on')

      % append file with coordinates
      plot(x,y,'r.','MarkerSize',20);

      % Increasing counter value
      counter = counter + 1;
      % Printing out points counter value
      text(x,y, strcat('#', int2str(counter)), 'color', 'r');

      % Output to file 
      fid=fopen(outputFile,'at');
      fprintf(fid, '%f %f %s %d\n', x, y, '; %', counter);
      fclose(fid);
  end
end

fopen
中的
w
权限会丢弃当前内容-因此在每次回调时,似乎都会丢弃现有文件。您可能应该(附加)
a
。。。也可以考虑<代码> t>代码>选项,因为您正在编写ASCII,而不是二进制。但是如果启用W,文件中还有一个最后一点,对吗?而且,尝试了wt,但仍然没有结果