The subplot function called in different matlab views - matlab

I have built a matlab application consisting of 2 views, each one knowing about the other's handle.
The main view launches the second view and gets its handle.
Then the function subplot() is called in the main view and an image is displayed using imshow().
What I would like to do is to display in the second view several details of the image, also by using the subplot() and imshow() functions.
Using the handle of the second view I call from the main view a function defined in the second view, which further calls subplot() and imshow() functions for details.
What happens is that initial image in the main view disappears being replaced with the details.
Can anybody give me an advice so that I can have the image displayed in the main view while the details are shown in the second view ?

Maybe you need to specify the axis in which you are placing your image or data. The imshow command allows you specify the axes to use for displaying images with the Parent parameter.
Combine this with the fact that subplot returns a handle to the axis it creates and you have.
h = subplot(m,n,i) %# create a subplot axes
imshow(img,'Parent',h) %# display an image in the previously created axis
Assuming you can pass information between the two sets of code you would need to retrieve the handle to the axis you want the image to show up in and use that handle in the call to imshow. Here is an example
hFig = figure;
h(1) = subplot(2,2,1)
h(2) = subplot(2,2,2)
setappdata(hFig,'axisHandles',h) %# stores the axes handles in appdata of the figure
....
Now when you want to show some an image in the first figure you can do the following assuming you know the handle to each of your figure windows.
h=getappdata(hFig,'axisHandles')
imshow(img,'Parent',h(1))

Related

How to get handle of the legend of a plot generated through plotyy when importing a fig file?

So my figures are here: http://www.atmos.uw.edu/~akchen0/CERES_Project/
I'd like to get the handle of the legend of the figure here so that I can copy the figure's legend entries when replotting the legend on a subplot.
If I try to find the axes by running findobj(ax1,'Type','axes','Tag','legend') though, it just returns an Empty matrix: 0-by-1. Is there another way to get the legend?
Your mistake was, that you used the axes handle instead of the figure handle:
leg = findobj(figureHandle,'Type','axes','Tag','legend')
or simply:
leg = findobj(gcf,'Tag','legend')
will work.
The setup is like this. First the figure is created. This one servs as a canvas. Then the other objects are drawn upon the figure. The objects drawn is called children. The function call,
plot(0:10);
Creates a figure and one child. The child is called an axes. By adding a legend with,
legend('This is a legend');
matlab creates an axes object named legend an places it in the figure. This becomes another child. The object handles to the children can be found in
h = get(gcf,'Children')
Then you may save the figure and close it down. When you are loading the figure again, the whole figure, with legends and so on is restored. This includes giving the children new object handles. This means that the children can again be retrieved with,
h = get(gcf,'Children')
you can find the child properties with
get(h(anyElementInChButJustOnePerCall))
By looking at the properties, you will notice that there is a porperty called Tag. This one is named legend for the legend. You can find it with get and look through the properties for all handles (should not be too many) or automatically with a for loop
h = get(hFig,'children');
hLeg = [];
for k = 1:length(h)
if strcmpi(get(h(k),'Tag'),'legend')
hLeg = h(k);
break;
end
end
Where hFig is the handle of the figure (written in the upper right "Figure..."). Or if the figure is just loaded it can be accessed with "get current figure" gcf.

How to use effectively radio buttons in a button group MATLAB GUI

I have 5 different filters as 5 different radio buttons in MATLAB GUI. I made them into a button group and now when i click the each button the noise image is shown through the axes. But i want to set the button group in such a way to show only one filter (one image). So, I followed this
(How to pass function to radio button in a button group created using guide in MATLAB?) which is given here at stackoverflow. But how do we "set" image in an axes.
I have attached the figure of my GUI.enter image description here
Thanks in advance
You "set" the image in an axes object using the normal plotting commands. Suppose the variable ax holds the handle to the axes object you want to draw on, you could write the following:
axes(ax); % Select the chosen axes as the current axes
cla; % Clear the axes
imagesc(im); % Now draw whatever you want - for example, an image.
Incidentally, in GUIDE you can get usually get the axes handle using the handles argument passed to all callbacks. So for example, if your axes are called axes1, your Button Group callback might look like this:
function uipanel1_SelectionChangeFcn(hObject, eventdata, handles)
ax = handles.axes1; % Get handle to axes
axes(ax); % Select the chosen axes as the current axes
cla; % Clear the axes
imagesc(rand(50) ); % Now draw

Display "imshow" images in a GUI using GUIDE

I have a piece of code that is 300 lines long. There is 3 different instances of imshow throughout the code that displays figures as the code is run..
The GUI I am creating will be very simple. Currently I have a push button that initiates the m file.
I am trying to get the images to be displayed within the GUI that I am creating and not in separate Figure windows.
I have being looking at tutorials online but cant get a quick fix for my problem, they all get a bit convoluted and I cant figure out what exactly to do.
I have 3 axis inserted onto the GUI, In the "view callbacks" for each axis I can createfcn, deletefcn and buttonDownFcn. When I createFcn it gives me a hint to "place code in OpeningFcn to populate axes1" in the auto generated code.
I have tried to do this but I am not able to find the correct place to write the code.
Can somebody tell me if I am going in the correct direction or if I have it wrong.
Thanks
In order to display these images, you need to declare the parent in imshow. The parent is what you want to act as the canvas for your image, and in your case will be an axes.
I created a very simple gui with three axes and a push button. MATLAB named my axes axes1, axes2 and axes3. Guide saves the handles to these axes so that you can interact with them throughout your gui code. For example, you mentioned the opening function... here is mine with a call to imshow (the only lines I added were the last three ones):
% --- Executes just before myGUI is made visible.
function myGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for myGUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes myGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
imshow('myImage1.png', 'Parent', handles.axes1)
imshow('myImage2.png', 'Parent', handles.axes2)
imshow('myImage3.png', 'Parent', handles.axes3)
Notice that I can grab the handles of my axes and then declare them to be the parents for the results of my imshow call.
If you're not sure what the names of your handles are, you can check in the GUI editor by right clicking, looking at the property inspector, and the tag property.
If you want to perform a similar operation for when you click on your pushbutton, right click on the button in the editor, and click on View Callbacks -> Callback, and you can add your imshow code there.
Good luck.
If I understand correctly you just want your images to appear in the axes instead of a figure. Try setting the focus to the axes itself before showing the image.
axes(1);%you may need to change the one to your axes handle.
imagesc(imageToBeDisplayed);
axes(2);
imagesc(secondImage);
axes(3);
imagesc(thirdImage);
This way before you call imagesc you make sure your program knows where to send the image. Otherwise it may just create a figure.

MATLAB ButtonDownFcn

I have a project of "Optical Character Recognition" in MATLAB and i need your help:
how do i recognize when the user press the mouse on an image?
i trying to do this with ButtonDownFcn but even when i just printing message
the message is not printed.
i want to allow the user to select the license plate from the image.
how can i do this and save the Pixels of the selected area?
thanks in advance.
Addressing your two questions:
I'm guessing that you are trying to set the 'ButtonDownFcn' of the figure window, which won't work how you expect it to. If you want to do something when the user clicks on an image, you should make sure that you're setting the 'ButtonDownFcn' of the image, and not the figure window or the axes object. Note this line in the figure property documentation (emphasis added by me):
Executes whenever you press a mouse button while the pointer is in the figure window, but not over a child object (i.e., uicontrol, uipanel, axes, or axes child).
This is why you have to set a 'ButtonDownFcn' for each object you want it to work for. Setting it for the figure window won't make it work automatically for each object in the figure. Here's an example that sets the 'ButtonDownFcn' for the figure and an image object:
img = imread('peppers.png'); %# Load a sample image
hFigure = figure; %# Create a figure window
hImage = image(img); %# Plot an image
set(hFigure,'ButtonDownFcn',... %# Set the ButtonDownFcn for the figure
#(s,e) disp('hello'));
set(hImage,'ButtonDownFcn',... %# Set the ButtonDownFcn for the image
#(s,e) disp('world'));
Notice how clicking inside and outside the image displays a different message, since each calls the 'ButtonDownFcn' for a different object. Notice also that if you click on the tick mark label of one of the axes, nothing is displayed. This is because the axes object has its own 'ButtonDownFcn', which wasn't set to anything.
If you have access to the Image Processing Toolbox you can use the function IMFREEHAND to have the user draw an ROI (region of interest) in the image. Then you can use the createMask method to create a binary mask of the image with ones for pixels inside the ROI and zeroes for pixels outside the ROI.

Problems with displaying edited pictures in a GUI created in MATLAB

I have an assignment to create a GUI using MATLAB GUIDE and am having a problem with displaying an edited picture. I need to have buttons that edit the picture (eg. remove red, blue, green components and rotate) and display that edited picture. I am using imshow to display the edited picture but it displays in a new window and shuts down the GUI I had running. Can anyone help?
I've been working on this and have tried numerous different ways of fixing the problem but none worked. However, I am using MATLAB 7.0.1, and 7.7.0 might have an update for this problem.
When you first plot the image with imshow, have it return a handle to the image object it creates:
A = (the initial matrix of image data);
hImage = imshow(A);
Then, to update the image with new data, try the following instead of calling imshow again:
B = (modification of the original image matrix A);
set(hImage, 'CData', B);
Using the set command will change the image object you already created (a list of image object properties can be found here).
Alternatively, you can also add additional parameters to a call to imshow to tell it which axes object to plot the image in:
hAxes = (the handle to an axes object);
imshow(A, 'Parent', hAxes);
EDIT:
Addressing your additional problem of sharing GUI data between functions, you should check out the MATLAB documentation here. As noted there, there are a few different ways to pass data between different functions involved in a GUI: nesting functions (mentioned on SO here), using the 'UserData' property of objects (mentioned on SO here), or using the functions setappdata/getappdata or guidata. The guidata option may be best for use with GUIs made in GUIDE.
The GUI m file functions automatically assign the image data to a variable called hObject. Once you have done your image alteration, you have to reassign the new data to hObject:
hObject = imshow(newimagedata)
Don't forget to update and save this operation by:
guidata(hObject, handles)