make an interactive graphic?

41 views (last 30 days)
dsq dq
dsq dq on 26 Jul 2021
Commented: Steven Lord on 26 Jul 2021
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 !

Accepted Answer

Steven Lord
Steven Lord on 26 Jul 2021
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 Comments
dsq dq
dsq dq on 26 Jul 2021
Yes it seems it's not the best but I don't really have the choice, my professor ask me to use it ...
I just have one another question and everything should be fine ! I have declared variables in a callback function and I would like to use again those variables but matlab write an error like "Unknown variable" (I don't remember the exact sentence. Should I use pointer or is there a way to celare variables to not be local to a function ?
Rik
Rik on 26 Jul 2021
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.

Sign in to comment.

More Answers (1)

Rik
Rik on 26 Jul 2021
Edited: Rik on 26 Jul 2021
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 Comments
Rik
Rik on 26 Jul 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
Steven Lord on 26 Jul 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.

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!