Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing with mouse on the GUI in matlab

I want to have a program in matlab with GUI, at run the program, user can draw anythings with mouse on the axes in GUI, and i want to saving created image in a matrix. how can i to do this?

like image 548
hamed aj Avatar asked Sep 21 '12 18:09

hamed aj


People also ask

How do you input a GUI in Matlab?

Users can enter scalar or vector values into inputdlg text edit fields. MATLAB® stores the input as a cell array of character vectors. Convert a member of the input cell array to a number, using str2num . Create an input dialog box that asks users to enter numerical data.

How do I edit a GUI figure in Matlab?

Type guide in command window. A new GUI dialog box will appear. In the dialog box you will select the existing GUI project. To to the tab and you will find the gui file which you want to edit.


3 Answers

Finally i find a good code and i have changed some parts for customizing for me. with this way, user can drawing anythings in the axes with mouse :

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','')
like image 72
hamed aj Avatar answered Sep 25 '22 21:09

hamed aj


The ginput function gets the coordinates of moueclicks within a figure. You could use these as points of a line, polygon, etc.

If this doesn't fit your needs you need to decribe what exactly you expect the user to draw.

For freehand drawing this might be helpful:

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

like image 39
Herr von Wurst Avatar answered Sep 25 '22 21:09

Herr von Wurst


The only way I know to interact with matlab windows using a mouse is ginput, but this will now let you draw anything with fluidity.

There are ways to use Java Swing components in matlab check http://undocumentedmatlab.com/ for more info.

EDIT: You may want to check this out as well.

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

like image 23
Erotemic Avatar answered Sep 21 '22 21:09

Erotemic