Drawing with the mouse in the graphical interface in Matlab

I want to have a program in matlab with a graphical interface, when the program starts, the user can draw anythings with the mouse along the axes in the graphical interface, and I want to save the created image in a matrix. How can i do this?

+6
source share
3 answers

Finally, I find some good code, and I changed some parts to customize for me. Thus, the user can draw anythings in the axes with the 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 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','') 
+8
source

The ginput function gets the coordinates of the moueclicks inside the shape. You can use them as points of a line, polygon, etc.

If this does not meet your needs, you need to describe what exactly you expect from the user.

For drawing, this may be useful:

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

+3
source

The only way I know to interact with Matlab windows with the mouse is ginput, but now it will let you draw anything with fluidity.

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

EDIT: You might also want to check this out.

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

+2
source

Source: https://habr.com/ru/post/925946/


All Articles