make an interactive graphic?

조회 수: 45 (최근 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
dsq dq
dsq dq 2021년 7월 26일
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 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
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
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
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 CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by