MATLAB Answers

make an interactive graphic?

조회 수: 5(최근 30일)
dsq dq
dsq dq 2021년 7월 26일
댓글: Steven Lord 2021년 7월 26일
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 !

채택된 답변

Steven Lord
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.
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
%create figure with image
%set callbacks;;
%store in guidata
function Callback(hObject,eventdata)
%load the guidata handles struct
  댓글 수: 8
Steven Lord
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.

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by