make an interactive graphic?
Mostrar comentarios más antiguos
Hey everyone !
I'm a french student (so sorry if my english is not perfect) and i am currently working on a GUI matlab.
I would like to create a matrice where I can click on some "boxes".
I already create a matrice (the photo is linked to the topic), and on this matrice I would like to click on one of the nine boxes and something will happen (like the boxe change of color).
I hope it was quite clear and understandable ! (if not you can ask me for some questions).
Thanks you very much in advance for your answer !
Respuesta aceptada
Más respuestas (1)
You can use the ButtonDownFcn property to trigger a function when the user clicks on an axes object.
You can use the CurrentPoint property of the figure to retrieve the position of the cursor. This is reported in the units defined in the Unit property of the figure. You then need to convert those to the units of your image.
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
Edit:
The code below doesn't work in the online editor (as that doesn't support interactivity), but you should not have any issues otherwise.
%get an example image
figure,IM=repelem(get(image,'CData'),10,10);close
%create figure with image
h.f=figure('Units','Normalized');
h.ax=axes('Parent',h.f);
h.im=imshow(IM,[],'Parent',h.ax);
%set callbacks
h.ax.ButtonDownFcn=@Callback;
h.im.ButtonDownFcn=@Callback;
%store in guidata
guidata(h.f,h)
function Callback(hObject,eventdata)
%load the guidata handles struct
h=guidata(hObject);
clc,get(h.f,'CurrentPoint')
end
8 comentarios
dsq dq
el 26 de Jul. de 2021
Rik
el 26 de Jul. de 2021
If you click on the axes (with either of the buttons), the ButtonDownFcn will run. You need to write a function that will do the rest of the work. Did you already set a ButtonDownFcn? Can you post that function here?
dsq dq
el 26 de Jul. de 2021
Steven Lord
el 26 de Jul. de 2021
Did you already have something plotted on the axes? If so did you click on the axes or on (or very near) the thing you plotted on the axes? The former should work, the latter not. If you want clicking on the thing you plotted to actually "count as" clicking on the axes, change the HitTest property of the thing you plotted to 'off'.
dsq dq
el 26 de Jul. de 2021
dsq dq
el 26 de Jul. de 2021
Rik
el 26 de Jul. de 2021
A lot of the bigger graphics functions will wipe the previous settings. imshow is one them. You should either use the primitives (image/line/patch/etc), modify the properties of your objects, or reset things like callbacks after every call.
Steven Lord
el 26 de Jul. de 2021
A lot of the bigger graphics functions will wipe the previous settings.
That's the default behavior. You could use hold on to change the NextPlot property of the figure and axes to 'add' (which will leave children alone and not reset properties.) Or you could manually change the NextPlot properties to 'add' or 'replacechildren' neither of which will reset the axes properties.
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


