Search code examples
matlabmatlab-guidematlab-gui

Passing values from radio buttons into the script in Matlab


So i created GUI in guide that looks like this:

GUI

I want to access data from radio button and then change the variables in my simulation (Bitrate and Modulation are the button groups, Improvement is a single radio button). For example - in the simulation I have a variable Rs=1e9, so when 1Gbps button is selected I want it to remain 1e9, but if 10Gbps button is selected I want it to change its value to 10e9.

Then after hitting Start button I want to start my simulation (which is in different .m file) with given parameters. How can I do it ? (I know about handles idea in matlab, but I don't know how to pass value to the simulation)

That's the code that controls gui - generated by guide. I added some code that starts simulation and close gui window.

function varargout = gui_final(varargin)


% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_final_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_final_OutputFcn, ...
                   'gui_LayoutFcn',  [], ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
   gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before gui_final is made visible.
function gui_final_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   unrecognized PropertyName/PropertyValue pairs from the
%            command line (see VARARGIN)

% Choose default command line output for gui_final
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes gui_final wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = gui_final_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject    handle to start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clc
close all

message = sprintf('Wait - this is a very long simulation!\nClick the OK button and wait');
uiwait(msgbox(message));
evalin('base', 'simulation');


% --- Executes on button press in dfe.
function dfe_Callback(hObject, eventdata, handles)
% hObject    handle to dfe (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of dfe


% --- Executes on button press in ook.
function ook_Callback(hObject, eventdata, handles)
% hObject    handle to ook (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of ook

Solution

  • You can do it using the object "hObject"

    Like this: Suppose that you have an slider, everytime the slider is moved the callback is called. You can use it to store a variable. Then, when you press a button you want to use that data for whatever. The code is:

    function slider_callback(hObject,eventdata)
        sval = hObject.Value;
        diffMax = hObject.Max - sval;
        data = struct('val',sval,'diffMax',diffMax);
        hObject.UserData = data;
        % For R2014a and earlier: 
        % sval = get(hObject,'Value');  
        % maxval = get(hObject,'Max');  
        % diffMax = maxval - sval;      
        % data = struct('val',sval,'diffMax',diffMax);   
        % set(hObject,'UserData',data);   
    
    end
    
    function button_callback(hObject,eventdata)
        h = findobj('Tag','slider1');
        data = h.UserData;
        % For R2014a and earlier: 
        % data = get(h,'UserData'); 
        display([data.val data.diffMax]);
    end
    

    REFERENCE: Matlab Docs

    UPDATE

    In your case you can do it with another approach if you want. When you press the button START you read the state of your radio buttons, and pass the appropiate value to your simulation_function that I suppose it is called "simulation". In the following example I suppose that you have a button-group called (tag): uibuttongroup1, and inside you have two buttons: radiobutton1 and radiobutton2

    % --- Executes on button press in start.
    function start_Callback(hObject, eventdata, handles)
    % hObject    handle to start (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    %Check which radio button is pressed
    switch get(get(handles.uibuttongroup1,'SelectedObject'),'Tag')
      case 'radiobutton1',  myParameter = 1e9;
      case 'radiobutton2',  myParameter = 10e9;
    end
    %Execute simulation
    clc
    close all
    
    message = sprintf('Wait - this is a very long simulation!\nClick the OK button and wait');
    uiwait(msgbox(message));
    evalin('base', 'simulation(myParameter)');