Using axes in Matlab GUI - matlab

My interest is to display image in axes, I've 2 GUI's input_window and fig5.
In my former GUI I've used one axes to display image using uigetfile ,
axes(handles.axes1);
imshow(fname);
Now by pressing pushbutton in 1st GUI it switches to fig5 GUI, where I've used so many axes to display multiple images, also i want to display fname image in one axes and it should automatically get displayed in fig5 GUI. For this I've used same above codes in Fig5_OpeningFcn
axes(handles.axes1);
imshow(fname);
I'm getting error like Undefined function or variable 'fname'.
please help me how to pass variables between GUI's

You could use setappdata and getappdata, like so:
% In the first figure:
setappdata(0, 'fname', fname);
% Show the second figure...
% In the second figure:
fname = getappdata(0, 'fname');
% Clear the appdata
setappdata(0, 'fname', '');
% Do stuff with fname

Related

Maltab: copy axes in a gui to a figure

I am going to save an image in the GUI to a sperate figure. Some of codes relevant are as follows:
axes(handles.axes1); %axes object
subplot(131); imshow(tempData(:,:,1),[]); title('I1');
subplot(132); imshow(tempData(:,:,2),[]); title('I2');
subplot(133); imshow(tempData(:,:,3),[]); title('I3');
%The three images are displayed in the GUI
% saved to a new figure
handles.axes1
figurenew = figure;
copyobj(handles.axes1,figurenew);
Then, there is an error when running the code:
Error using copyobj
Copyobj cannot create a copy of an invalid handle.
Does that means the handle handle.axes1 no longer exist? Then how to modify the codes to save the displayed image in the GUI?
Each subplot has its own Axes object.
You can get this Axes object by writing as follow.
figure;
axes1 = subplot(131);
Then you can copy the object as you wrote.
figurenew = figure;
copyobj( axes1, figurenew );

Matlab getrect function - How to skip the mouse input

In Matlab GUI, I need a user mouse input as a rectangle that I have to plot on the axes1.
For this, I have code below:
axes(handles.axes1);
filename = 'A';
img = imread(filename);
imshow(img);
hold on;
rect_cord = getrect(handles.axes1);
rectangle('Curvature', [0 0],'Position', [rect_cord],'EdgeColor',[1 0 0]);
This code runs fine (takes user input and plots the rectangle). However, for some images I don't want to get the user input from mouse (using getrect). In this case, how to skip the getrect function and move on to next image?
I have a pushbutton ("next"), I want to show next image when push button is pressed instead of taking user input.
Thanks,
I will try to rephrase and modify a bit: you want to draw a rectangle only if one clicks on the axes or image.
Therefore:
First of all I would suggest to put the getrect-part into another function. This function should only be triggered if one clicks on the image. The so called "ButtownDownFcn" seems to be suitable for this job. When using GUIDE, you will find it double-clicking on the axes within the property-inspector that is popping up.
Then put the getrect-part into this function:
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)
rect_cord = getrect(handles.axes1);
rectangle('Curvature', [0 0],'Position', [rect_cord],'EdgeColor',[1 0 0]);
Now I thought that this is all that has to be done. But testing this with a plot or image within the axes prooved me wrong :o
Sadly one has to redirect the children of the axes to this function, because by default, it is only assigned to the background of the axes (=the blank axes).
by searching the net, I found the following solution here:
Matlab in Chemical Engineering: Interacting with your graph through mouse clicks
It works like this:
when plotting the image, you have to add the line
% and we also have to attach the function to the children, in this
% case that is the line in the axes.
set(get(gca,'Children'),'ButtonDownFcn', #mouseclick_callback)
Hope that helps!

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

pass filename to gui to show image

I have a gui which already has axes on it...i want to pass a filename to that gui and display it on the axes...here's how i tried it..this is written in an xyz.m file:
close current_gui;
result_image(im2fn);
here, current gui is a gui i was working with and result_image is the gui where i want to show the image, im2fn is a variable having the filename of the image...
In result_gui i wrote the following code..
function result_image_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
axes(handles.axes1);
imshow(im2fn);
The gui is displayed but there is no image shown and also i get an error as Undefined function or variable 'im2fn'....Please Help!!
You are receiving this error because im2fn is not part of the local workspace inside OpeningFcn - you have made an incorrect assumption about the way varargin works. When you use varargin, it will be a cell array of the values you passed. See the link for more examples, but in your case you just want to do something like this:
% Make sure the user passed us something to display
assert(~isempty(varargin), [mfilename ':NoImageToDisplay'], 'You must pass an image to the ''result_image'' GUI');
% Extract data from varargs
im2fn = varargin{1};
% Show it!
imshow(im2fn);

In Matlab, own function which dynamically load/display images into Axes from a Gui in a loop?

With Guide I made a Matlab Gui that have 10 Axes in which i want to display images, in all of them at once, after i press a Button.
I made a separate .m file with function Load_Write_Img_Results(img_index) that i call from the Button Callback which have the following code:
for i = 1 : 10
handles_imgOut = findobj('Tag', ['imgOut' num2str(i)]);
set(handles_imgOut, 'HandleVisibility', 'ON');
axes(handles_imgOut);
image(imgs_data{img_index(i)});
...
end
Every time when i run the main Gui and press the button for the first time, images are displayed in all axes, so everything works ok.
The problem appear when i press the button for the second time and i get this error:
Error using axes
Invalid object handle
on this line:
axes(handles_imgOut);
When debugging, i saw that after handles_imgOut = findobj('Tag', ['imgOut' num2str(i)]); the handles_imgOut is not getting any value and is empty, so it's obvious that error.
Is there any chance i can't get handles for axes at the second press of the button?
Also, i want to know how can i solve this warning from Matlab:
Calling AXES(h) in a loop can be slow. Consider moving the call to AXES outside the loop.
Thanks in advance, any suggestions are welcome!
[SOLUTION]:
for i = 1 : 10
handles_imgOut = findobj('Tag', ['imgOut' num2str(i)]);
set(handles_imgOut, 'HandleVisibility', 'ON');
axes(handles_imgOut);
image(imgs_data{img_index(i)});
set(gca, 'Tag', ['imgOut' num2str(i)]); //! renew the tag
...
end
I'm fairly new to GUIDE and I've encountered similar problems, with the graph not being updated more than once / axes not found.
In the end, I used the following approach, which I hope can be useful to you too:
% I get my axes directly from the handles object, instead of using findObj:
graph1 = handles.axes1;
graph2 = handles.axes2;
% clear axes
cla(graph1);
cla(graph2);
% showTrial is my function that draws the graphs -
%notice that I used handles to store other variables as well (.data, .trials)
showTrial(handles.data, handles.trials(1), graph1, graph2)
To sum it up:
don't use findObj, get your axes from handles (it should automatically contain them as imgOut1,imgOut2, etc.)
pass the axes to your drawing function or pass directly the handles variable e.g. Load_Write_Img_Results(img_index, handles)
The fact that it works on the first button press, but not thereafter hints that
image(imgs_data{img_index(i)});
opens new axes instead of drawing into the existing ones. Since the new axes are not initialized with your tags, findobj will not find them. Either
(1) make sure to hold the original axes by placing a hold on command right after their creation,
or
(2) renew the tag right after the image command by
set(gca, 'Tag', ['imgOut' num2str(i)]);