Use selected radio button in another function - matlab

I have GUI created with GUIDE. I have 3 radiobuttons in a group. When I run the program and after I enter in values for some calculations I get plots for pressure, temperature, lift/drag force (dependent on which radiobutton I push). The structure of program is as follows:
Main code (GUI):
passes values to a function named Apollo after the "Calculate" button is pushed.
Apollo code:
contains sub functions to calculate necessary values for the calculation of the pressure, temperature, and lift/drag force. Last sub function is called plot_data. This function plots the shape of the space craft in one figure and plots the pressure, temperature, lift/drag force in another figure upon selection of the corresponding radiobutton.
Problem - I'm new at programming a GUI. I'm having problems figuring out how to determine which radiobutton is selected from the GUI and using some sort of logic as a switch in my plot_data function. I would like for the select case to be in the plot_data function.
Any help would be appreciated.

Let's say your radiobutton handles are rb1 and rb2 and they are stored in handles structure. Then, you can check if they are selected using the 'Value' property.
function apollo(hObj, eventdata, handles)
if get(handles.hrb1, 'Value')
% do stuff
elseif get(handles.hrb2, 'Value')
% do other stuff
end
end
Update: Response to OP's comment about SelectionChangeFcn:
If you do not have a lot of data you can plot them all at the beginning and then change the Visible properties of the plots.
function cbSelectionChange(hObj, eventdata, handles)
rbTag = get(eventData.NewValue, 'Tag');
switch rbTag
case 'rb1'
set(handles.plotHandle1, 'Visible', 'On');
set(handles.plotHandle2, 'Visible', 'Off');
case 'rb2'
set(handles.plotHandle2, 'Visible', 'On');
set(handles.plotHandle1, 'Visible', 'Off');
end
end
If you do have a lot data you can call plot_data function from SelectionChangeFcn callback.
function cbSelectionChange(hObj, eventdata, handles)
rbTag = get(eventData.NewValue, 'Tag');
switch rbTag
case 'rb1'
plot_data(yourArguments1);
case 'rb2'
plot_data(yourArguments2);
end
end
Performance of your code will not be affected by passing the data unless you modify it. Doug Hull says here:
MATLAB uses a system commonly called "copy-on-write" to avoid making a
copy of the input argument inside the function workspace until or
unless you modify the input argument. If you do not modify the input
argument, MATLAB will avoid making a copy.

If you want to do something after user clicks on a radiobutton, you should edit "callback" property of your object.
If you want to test the state of radiobutton from your code, your should check "Value" property. It would be one for selected button and zero otherwise.
Look through this tutorial

Related

How to use "ButtonDownFcn" in a populated GUI axes?

I have a very simple GUI made in guide where i have a plot function initiated by a pushbutton which plots a scatter plot in axes (called Method1axes1):
handles.plot = scatter(X,Y, 'parent', handles.Method1axes1);
Now I want the user to be able to click the axes (plot) to get en new larger figure. I tried the below code which is working if i DON'T plot in the axes first. As soon as I run the plot function the scatterplot appears in Method1axes1 but I can no longer click the figure.
% --- Executes on mouse press over axes background.
function Method1axes1_ButtonDownFcn(hObject, eventdata, handles)
figure
scatter(X,Y);
What am I doing wrong?
This is a kind of special case for MATLAB, and it is not extremely well documented.
There are 2 things you need to consider:
1) The most obvious part. When you plot something in your axes, the plot is on the foreground. So when you click on your axes, the top plot intercept that click and tries to process it. You need to disable the mouse click capture from the plot/scatter/image objects you have in your axes. For that, you have to set the HitTest property of your scatter object to 'off'. (recent MATLAB version have changed the name of this property, it is now called PickableParts).
2) Much less obvious and documented. It used to be in the doc for the axes ButtonDownFcn callback but it is not explained anymore (although the behaviour persist). This is what I could find on old forums:
When you call PLOT, if the axes NextPlot property is set to 'replace'
(which it is by default) most of the properties of the axes (including
ButtonDownFcn) are reset to their default values.
Change the axes NextPlot property to 'replacechildren' to avoid this,
or set the ButtonDownFcn after calling PLOT, or use the low-level LINE
function instead of the higher-level PLOT function.
This is also discussed and explained here: Why does the ButtonDownFcn callback of my axes object stop working after plotting something?
For your case, I tried set(axe_handle,'NextPlot','replacechildren') and it works ok to let the mouse click reach the ButtonDownFcn, but unfortunately it creates havoc with the axes limits and LimitModes ... so I opted for the second solution, which is to redefine the callback for ButtonDownFcn after every plot in the axes.
So in summary, your code for the pushbutton1_Callback should be:
function pushbutton1_Callback(hObject, eventdata, handles)
% Whatever stuff you do before plotting
% ...
% Plot your data
handles.plot = scatter(X,Y, 'parent', handles.Method1axes1);
% Disable mouse click events for the "scatterplot" object
set(handles.plot,'HitTest','off') ;
% re-set the "ButtonDownFcn" callback
set(handles.Method1axes1,'ButtonDownFcn',#(s,e) Method1axes1_ButtonDownFcn(s,e,handles) )
And for your axes mouse click event, you might as well keep the handle of the new generated objects:
function Method1axes1_ButtonDownFcn(hObject, eventdata, handles)
handles.newfig = figure ;
handles.axes1copy = copyobj( handles.Method1axes1 , handles.newfig ) ;
Note that instead of plotting a new set, I simply use the copyobj function, very handy when you need to reproduce a plot.
Illustration:
If you want to set the figure/graph to enlarge and shrink on mouse
scroll/click, then just set the zoom property of the required axes in
OpeningFcn within the m file.
For example within the OpeningFcn in the GUI's m file, put the below code. Please ensure that you put the below code within the OpeningFcn function.
h1 = zoom(handles.Method1axes1);
h1.Enable = 'on';
Now, on each mouse scroll/click, you would be able to zoom in/out the graphs.
A sample screenshot of openingFcn for a GUI named ZoomAxesDemo is given below.

matlab set_param function doesn't work in loop

I have a Matlab GUI code that lets you draw on axes and passes the coordinates to a constant in Simulink. While the mouse button is held down it should draw on the axes and send the coordinates and when it'not, it should send the coordinates but it should not draw. Here is the code:
`
function figure1_WindowButtonUpFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global bool;
bool=false;
set(handles.figure1,'WindowButtonMotionFcn',#(hObject,eventdata)figure1_WindowButtonMotionFcn(hObject,eventdata,guidata(hObject)));
%set the WindownButtonMotionFcn back in order to make it work again
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
set(handles.figure1,'WindowButtonMotionFcn',#empty); %change the windowbuttonmotionfcn in order not to let it work
global bool;
bool=true;
global lastX;
global lastY;
x=0;
while bool
coord=get(handles.axes4,'CurrentPoint');
if coord(1)<0.003
coord(1)=0.003
x=0;
end
if coord(1)>1
coord(1)=1
x=0;
end
if coord(3)<0
coord(3)=0
x=0;
end
if coord(3)>0.95
coord(3)=0.95
x=0;
end
if x>1
arrayX=[lastX coord(1)];
arrayY=[lastY coord(3)];
line(arrayX,arrayY);
set_param('dosya_yukle_deneme/Constant','value',num2str(coord(1)));
end
x=x+1;
lastX=coord(1);
lastY=coord(3);
drawnow;
end
function empty(~,~,~)
% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(hObject, eventdata, handles)
coord=get(handles.axes4,'CurrentPoint');
set_param('dosya_yukle_deneme/Constant','value',num2str(coord(1)));
While the mouse button is pressed down, it draws the lines but the set_param function doesn't work. However, the one in the figure1_WindowButtonMotionFcn works pretty well when needed. It seems like the problem is the while loop. Any help would be appreciated.
You can't run your while loop within the figure1_WindowButtonDownFcn callback because the Matlab GUI is single-threaded. This means the while loop is blocking the Matlab GUI and preventing things from updating correctly. You need to let the callback return in order for Matlab to be able to update the GUI. This is a general rule for GUI callbacks in Matlab; whatever you do in the callback will block the GUI.
In fact you don't need a while loop at all, because WindowButtonMotionFcn will call back every time the cursor changes. You put the code inside the loop into the figure1_WindowButtonMotionFcn callback. You will also need an extra global flag indicating whether the button is down or not, but that is easy to create. figure1_WindowButtonDownFcn should set the button down flag, and figure1_WindowButtonUpFcn resets the button down flag. Then figure1_WindowButtonMotionFcn checks whether the button down flag is set, and if it is, then it executes the code within your while loop.
I solved the problem! So I found out that the code was working since the beginning. I had connected the constant to a display, while the figure1_WindowButtonMotionFcn was working it showed the value but not while the other one was working. It seems to be that it's a bug in MATLAB UI; the display doesn't renew itself when a mouse button is detected.

GUI in matlab, loop of images in axes

So I'm building a classifier of images. In the GUI a image loads and insert a value on a text box, and push a button. I'm having a problem loading the image in the axes. Because when the axes function is called the handles is zero(due to:% handles empty - handles not created until after all CreateFcns called). And my problem is, how do I get to just call one image at a time for the axes.
The ideal solution, is I create a handles.images=imagedatastore, and every time I push the button I add to a counter(which I already have made) and then that give the indices to get the image from the datastore. My problem with this is that I can't get the first picture, because in the beginning the handles are empty. I have made the callfunction for the axes:
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
[pict_croped,Nphotos,Date_img] =getcropedimagages;
handles.img =pict_croped;
i=readimage(handles.img,1);
% axes(hObject)
imshow(i)
% Hint: place code in OpeningFcn to populate axes1
but this has two problems, first, I don't really want to call the function that creates the datastore all the time I push the button, second, I still can't get the indice of the counter to be in the function,if I have:
i=readimage(handles.img,handles.counter)
it will give me the error in the first time, of not having handles.counter
Any idea how to solve this?This is the first GUI I'm building.
The issue is very clearly in the comment that GUIDE provides for you. The handles struct isn't populated until all CreateFcn have been run so you'll want to use the OpeningFcn to do any initialization of the graphics objects. You can then add any data you need to the handles struct and save it using guidata so that it's available from within all of your other callback functions.
function OpeningFcn(hObject, eventData, handles)
[pict_croped,Nphotos,Date_img] = getcropedimagages;
handles.img = pict_croped;
i = readimage(handles.img,1);
imshow(i, 'Parent', handles.haxes1)
% "Save" the changes to the handles object
guidata(hObject, handles)
Well, I end up with:
in the opening fucntion:
i = readimage(handles.img,handles.counter);
imshow(, 'Parent', handles.axes1)
and in the button call back:
i = readimage(handles.img,handles.counter);
imshow(i, 'Parent', handles.axes1)
inthe end is a very simple solution, I think I was just mind blocked over the first iteration...

unable to plot on axes in matlab gui from listbox

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.

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)]);