make an interactive graphic?
73 次查看(过去 30 天)
显示 更早的评论
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 !
0 个评论
采纳的回答
Steven Lord
2021-7-26
Before plotting the ButtonDownFcn works well but after plotting nothing happen.
So you did something like this?
ax = axes;
ax.ButtonDownFcn = @(varargin) disp('hi');
% Click on the axes, see that 'hi' is displayed
plot(ax, 1:10, 1:10);
% Click on the axes, nothing is displayed
If so that's correct. From the description of the NextPlot property for axes, "Properties to reset when adding a new plot to the axes, specified as one of these values:" The default behavior is "'replace' — Delete existing plots and reset axes properties, except Position and Units, to their default values before displaying the new plot."
You could use hold on before the plot call to not reset the axes ButtonDownFcn property or you could set the ButtonDownFcn of the axes after calling plot not before. Alternately depending on what you're plotting you may be able to call a lower-level plotting function (line instead of plot.)
But without turning the HitTest property for the line created by either plot or line to 'off' clicking exactly ON the line (or I think within a pixel or three of it) won't trigger the ButtonDownFcn function even if it is set.
Though with that picture, do you need to click on an axes? I'd consider making a 3-by-3 grid of buttons and giving each button an appropriate callback.
7 个评论
Rik
2021-7-26
Can you ask your professor why they're teaching bad tools? There could be a reason, but I don't really see the point.
As for your question: every function is a separate function with its own variables. If you want to share variables between different functions of a GUI you should store them in fields of the guidata struct. There are ways to have variables be available in multiple functions, but you should not be using them as long as you have alternatives.
更多回答(1 个)
Rik
2021-7-26
编辑:Rik
2021-7-26
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 个评论
Rik
2021-7-26
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
2021-7-26
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!