I want to program the 'axes1' section of a new GUIDE project in a way that it scatters the points inside the axes when the user clicks on them.
I am new to GUIDE and I don't fully understand how hObjec, handles, etc. work. What I have gathered so far from other posts is the following piece of code:
% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%axesHandle = get(hObject,'Parent');
coordinates = get(hObject,'CurrentPoint');
coordinates = coordinates(1,1:2);
scatter(coordinates(1),coordinates(2));
I am not using the 'get(hObject,'Parent')' section because that was used in the case you use an image to display in the axes and that is not my case.
Can someone please explain how can I do this?
Thanks in advance for your time
I solved this months ago but I didn't post the solution!
It was easier than I thought, I just had to store each mouse press as a (x, y) point and then use plot:
% ND = Nodes in the network
ND = zeros(N,2);
ND(1,:) = ginput(1);
plot(ND(1,1),ND(1,2),'ko','MarkerSize',12);
hold on;
for i=2:N
ND(i,:) = ginput(1);
plot(ND(i,1),ND(i,2),'ko','MarkerSize',12);
end
Related
I am attaching a sample GUI codes, which has two axes with 2 images and when I use ginput to select seed point I am able to select on either axes, Is there anyway to limit the ginput to a specific axes
% --- Executes on button press in open.
function open_Callback(hObject, eventdata, handles)
% hObject handle to open (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global img1;
global img2;
img1 = imread('peppers.png');
img2 = imread('rice.png');
axes(handles.axes1);
imshow(img1,[]);
axes(handles.axes2);
imshow(img2,[]);
% --- Executes on button press in seedpointSelect.
function seedpointSelect_Callback(hObject, eventdata, handles)
% hObject handle to seedpointSelect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global img1;
global img2;
global x;
global y;
axes(handles.axes1);
imshow(img1,[]);
[y,x] = ginput(handles.axes1);
y = round(y); x = round(x);
set(handles.xcord,'String',num2str(x));
set(handles.ycord,'String',num2str(y));
Any help on limiting ginput to a specific axes,
Thanks,
Gopi
In older versions of MATLAB you used to be able to change the HitTest property of the axes to ignore the click from ginput
set(handles.axes2, 'Hittest', 'off')
The better approach though is to use ButtonDownFcn as you have much more control over mouse events with an axes object.
From within your OpeningFcn
set(handles.axes1, 'ButtonDownFcn', #mouseCallback)
Then you'll need to create the callback function
function mouseCallback(src, evnt)
handles = guidata(src);
% Get the current point
xyz = get(src, 'CurrentPoint');
x = xyz(1,1);
y = xyz(1,2);
% Store x/y here or whatever you need to do
end
Don't use ginput, create a mouse click callback instead (ButtonDownFcn). You can set the callback to, for example, remove the callback function from the axis. In your main program, which sets the callback, you then waitfor that property to change. As soon as the user clicks, you get control back, and you can then read the location of the last mouse click (CurrentPoint). Note that the position you read out is in axis coordinates, not screen pixels. This is a good thing, it most likely corresponds to a pixel in the image displayed.
I am working on MATLAB GUI in which i am updating the work-space variables in a list box and then trying to plot them on axes in GUI.
I have one other push button for performing plotting operation. But when i click on plot button, i get plots in a figure which pops up.
But according to my application i have to create the plots in axes. I am unable to do so
Kindly help
MY plot button code is as follows:
function plot_button_Callback(hObject, eventdata, handles, varargin)
% hObject handle to plot_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[x] = get_var_names(handles);
if isempty(x)
return
end
if isequal(x,'a')
% figure(gcf)
try
figure(1)
evalin('base',['plot(a,b,''--r'')'])
hold all
evalin('base',['plot(a,c,''k'')'])
hold all
evalin('base',['plot(a,d,''g'')'])
figure(2)
evalin('base',['plot(a,e,''g'')'])
hold all
grid on
catch ex
errordlg(...
ex.getReport('basic'),'Error generating linear plot','modal')
end
Within each GUI callback, you have a variable called handles which is the key to editing/accessing any item in your GUI. In the case of plotting to an existing axis, you need to add an additional argument to the plot function. Here's a line of code I yanked from a GUI that I wrote:
plot(handles.axes1, xdata, ydata);
Now, this approach might not work easily for you because you are using the evalin function (which I don't recommend doing, it'd be much better to pass that information in to the gui). Regardless, a good way to implement your goal with these constraints is
a = evalin('base','a');
b = evalin('base','b');
plot(handles.axes1,a,b,'--r');
Your GUI axes might not be named axes, you'll have to check on that. You should also probably remove the figure(1) call, if I understand your goal correctly.
Also, you don't need to invoke hold all after each time you plot, once is sufficient.
I'm working on a GUI able to display images and data associated with the latter images.
I have an x,y image, and a function f(x,y) (wich is a contour) and I'd like to show both the image and the contour in one single plot using an axes object.
This is how I get the image showed in the axes :
function aff_toto_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin
imshow(handles.im_ref,'parent',handles.axes1);
Now I get a contour matrix :
[handles.c,handles.h] = contour(handles.coor_y,handles.coor_x,handles.fun,handles.vec_iso);
I'd like to plot these contour lines on the image itself, in handles.axes1. Does anyone have a clue ?
Thank you all for reading this.
EDIT :
For now I'm just tying to plot some random sine over my picture. I tried
imagesc(handles.im_ref,'parent',handles.axes1);
hold(handles.axes1,'on');
plot(handles.axes1,handles.coor_x,sin(handles.coor_x));
hold off;
which shows the picture but the plot remains invisible.
I eventually found out where my problem was. using gui objects the hold function has to be used in a different way. As shown below you can specify on whether object hold is working.
It is the same for the contour function. It is possible to declare the Parent property (which can be an axes).
function aff_toto_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin
imagesc(handles.im_ref,'parent',handles.axes1);
hold(handles.axes1,'on');
set(handles.axes1,'color','none');
[handles.c,handles.h] = contour(handles.coor_y,handles.coor_x,handles.ch_tr,handles.vec_iso,'Parent',handles.axes1);
hold(handles.axes1,'off');
Using this code, I was able to solve my problem. See also this page in order to find further information about the issue of plotting a random curve on a random picture (image flip issues are discussed there for instance).
I need to apply it for two different plots on user button press not only the last one.
.I have wanted to ask is there some kind of a command that i could select plot from subplot and than proceed with it?
I have GUI and GUI displays me two 3d plots, but when i need to press some buttons for example which changes the view of those plots only one plot is changing the viewing angles other one is doing nothing.
So is there any tool to make both plots change?
function byheight_pushbutton_Callback(hObject, eventdata, handles)
% hObject handle to byheight_pushbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Display contour plot of the currently selected data
%select the first plot
view(90,0), shading interp
%select the second plot
view(90,0), shading interp
Use the syntax:
view(ax,...)
where you can specify which axes you call the view function on. For more details, see the documentation page for the view function.
Arnaud
I am using Matlab's GUIDE for the first time, I am trying to edit one of the two push button functions (both open an image), but editing one changes all of them. Here is a bit of code:
% --- Executes on button press in Floating.
function Floating_Callback(hObject, eventdata, handles)
clc;
axes(handles.axes1);
[Float, PathName, FilterIndex] = uigetfile('*.bmp');
if(Float ~= 0)
Floating = fullfile(PathName, Float);
FloatArray = imread(Floating);
imshow(FloatArray);
axis on;
end
% Update handles structure
guidata(hObject, handles);
% hObject handle to Floating (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in Reference.
function Reference_Callback(hObject, eventdata, handles)
clc;
axes(handles.axes2);
[Ref, PathName, FilterIndex] = uigetfile('*.bmp');
if(Ref ~= 0)
Reference = fullfile(PathName, Ref);
ReferenceArray = imread(Reference);
image(ReferenceArray);
end
% Update handles structure
guidata(hObject, handles);
For example,
image(ReferenceArray)
will open an image in RBG, but
imshow(FloatArray)
will open in grayscale (I also do not understand why that is). But my main concern is after opening up
imshow(FloatArray)
the other image will automatically turn grayscale. I am very confused... Also, as far as I know the images ARE already grayscale, at least they are when I open them in MS paint or ImageJ.
It would be better to explicitly specify the parent handle whenever you are doing GUI stuff. For example:
imshow(img, 'Parent',handles.ax1)
and
axis(handles.ax1, 'on')
As for images and colormaps, you should understand the type of images MATLAB supports (indexed vs. truecolor). Also note that a figure has only one colormap applied to all images, although there are techniques to overcome this.