0
我讀過幾篇關於如何將數據輸出到文件的文章,但它似乎並不適合我。我有我的matlab文件駐留在同一文件夾的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
該項目是關於如何獲取圖像鼠標點擊座標和輸出點座標文件。
的'w'許可,不得以'fopen'放棄當前的內容 - 因此它似乎是在逢回調現有的文件將被丟棄。你可能應該(附加)'a'而且考慮't'選項,因爲你正在編寫ascii而不是二進制文件。 –
但啓用w後,應該還有剩餘的文件中的最後一個點,對吧?此外,嘗試wt,但仍然沒有 – Benas