2012-09-21 13 views
6

GUIを使ってmatlabにプログラムを作成したい、プログラムを実行するときに、GUIの軸にマウスで任意のものを描くことができ、作成した画像を行列に保存したい。どうすればこのことができますか?matlabのGUIでマウスを使って描画

答えて

8

最後に、私は良いコードを見つけて、私は私のためにカスタマイズするためのいくつかの部分を変更しました。このように、ユーザーがマウスでの軸でanythingsを描くことができます。

function userDraw(handles) 
%F=figure; 
%setptr(F,'eraser'); %a custom cursor just for fun 

A=handles.axesUserDraw; % axesUserDraw is tag of my axes 
set(A,'buttondownfcn',@start_pencil) 

function start_pencil(src,eventdata) 
coords=get(src,'currentpoint'); %since this is the axes callback, src=gca 
x=coords(1,1,1); 
y=coords(1,2,1); 

r=line(x, y, 'color', [0 .5 1], 'LineWidth', 2, 'hittest', 'off'); %turning  hittset off allows you to draw new lines that start on top of an existing line. 
set(gcf,'windowbuttonmotionfcn',{@continue_pencil,r}) 
set(gcf,'windowbuttonupfcn',@done_pencil) 

function continue_pencil(src,eventdata,r) 
%Note: src is now the figure handle, not the axes, so we need to use gca. 
coords=get(gca,'currentpoint'); %this updates every time i move the mouse 
x=coords(1,1,1); 
y=coords(1,2,1); 
%get the line's existing coordinates and append the new ones. 
lastx=get(r,'xdata'); 
lasty=get(r,'ydata'); 
newx=[lastx x]; 
newy=[lasty y]; 
set(r,'xdata',newx,'ydata',newy); 

function done_pencil(src,evendata) 
%all this funciton does is turn the motion function off 
set(gcf,'windowbuttonmotionfcn','') 
set(gcf,'windowbuttonupfcn','') 
+0

これらの関数を使用して描画するにはどうすればよいですか? – mikeglaz

3

ginput関数は、figure内のmoueclicksの座標を取得します。

これがあなたのニーズに合わない場合は、ユーザーが描くと予想される内容を明確にする必要があります。

手描きの場合、これは役に立つかもしれません:

http://www.mathworks.com/matlabcentral/fileexchange/7347-freehanddraw

+0

私は、ユーザの描画は、ユーザーが英数字の文字を描画することができるはずという文字を検出するためのプログラムを開発しています。 –

+0

上記の私の編集を参照してください。 –

2

私はMathWorks社のMATLABウィンドウはマウスを使用して対話するために知っている唯一の方法は、関数ginputであるが、これは、今あなたが流動性を持つ何かを描くようになります。

matlabチェックhttp://undocumentedmatlab.com/でJava Swingコンポーネントを使用する方法があります。

編集:これもチェックしてください。

http://blogs.mathworks.com/videos/2008/05/27/advanced-matlab-capture-mouse-movement/

関連する問題