Search code examples
matlabuser-interfaceuser-inputmatlab-figurematlab-gui

Supply inputs to a GUI and catch the output in order to use it in another function


I have created a GUI function as follows. what I am trying to do is to put this GUI in a loop so that I can use it for different elements. for the output I have two vectors that are 6 by 1. What I am trying to do is, when I choose different radio buttons and put different values in the edit text of the GUI, to have the results saved in different positions of the output vector depending on the radio buttons. I am trying to give the GUI a title as the input.

Thanks in advance.

function varargout = distributedloads(varargin)
% DISTRIBUTEDLOADS MATLAB code for distributedloads.fig
%      DISTRIBUTEDLOADS, by itself, creates a new DISTRIBUTEDLOADS or raises the existing
%      singleton*.
%
%      H = DISTRIBUTEDLOADS returns the handle to a new DISTRIBUTEDLOADS or the handle to
%      the existing singleton*.
%
%      DISTRIBUTEDLOADS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DISTRIBUTEDLOADS.M with the given input arguments.
%
%      DISTRIBUTEDLOADS('Property','Value',...) creates a new DISTRIBUTEDLOADS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before distributedloads_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to distributedloads_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help distributedloads

% Last Modified by GUIDE v2.5 28-Feb-2016 14:52:56

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @distributedloads_OpeningFcn, ...
                   'gui_OutputFcn',  @distributedloads_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 distributedloads is made visible.
function distributedloads_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   command line arguments to distributedloads (see VARARGIN)

imshow('disloads.png')
% Choose default command line output for distributedloads
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = distributedloads_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)
varargout = str2double(handles.DATA.EL);

function ELNUM_Callback(hObject, eventdata, handles)
% hObject    handle to ELNUM (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ELNUM as text
%        str2double(get(hObject,'String')) returns contents of ELNUM as a double
EL = get(hObject , 'String');

handles.DATA.EL = EL;
guidata(hObject,handles)



% --- Executes during object creation, after setting all properties.
function ELNUM_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ELNUM (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function LOADVAL_Callback(hObject, eventdata, handles)
% hObject    handle to LOADVAL (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of LOADVAL as text
%        str2double(get(hObject,'String')) returns contents of LOADVAL as a double

Load = get(hObject , 'String');
handles.DATA.Load = Load;
guidata(hObject , handles)
% --- Executes during object creation, after setting all properties.
function LOADVAL_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LOADVAL (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



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

delete(handles.figure1)

% --------------------------------------------------------------------
function buttongroup_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to buttongroup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Type = get(hObject , 'selectedObject');
handles.DATA.Type = Type;
guidata(hObject , handles)

Solution

  • I wrote an example script that creates GUI and a one callback function.

    In the GUI there is vector of handles to Radio Buttons, Push Button and axes with line to visualize the results.

    The script code:

    close all          % close all figures
    figure             % open a figure for GUI
    Values=zeros(3,1); % Variable of the interest
    
    UIGroup=uibuttongroup('parent',gcf,'position',[0 0 1 1]); % Group for Radio Buttons
    for ii=1:3         % create 3 Radio buttons, for example
      RB(ii)=uicontrol('style','radiobutton',...
                       'units','normalized','position',[.05, ii/10 0.15 0.1],...
                       'parent',UIGroup,'string',['Button ' num2str(ii)]);
    end
    % Push Button that runs DoIt function
    uicontrol('style','pushbutton','string','DO',...
              'units','normalized','position',[0.45 0.05 0.1 0.1],...
              'callback','Values=DoIt(RB,L,Values);')
    
    % Axes and Line just for example
    ax=axes('units','normalized','position',[0.25 0.2 0.6 0.7],...
            'xlim',[-0.1 3.1],'ylim',[-0.1 1.1]);
    L=line('xdata',1:3,'ydata',Values,'marker','.','linestyle','none')
    

    This script defines Values variable and content of the GUI.
    Push Button runs the DoIt function which assigns new content to Values according to RB handle. Values are needed only to keep other values in Values, L is used only to visualize the changes.

    DoIt code:

    function[OutValues]=DoIt(RadioHandle,LineHandle,InValues)
        OutValues=InValues;        % Copy Values from input to output variable
    %% Find which radio button is active
        M=max(size(RadioHandle));
        for ii=1:M
            Radios(ii)=get(RadioHandle(ii),'value');
        end
        RadioChecked=find(Radios==1);     % This RadioButton is active
    
        OutValues(RadioChecked)=ProcessIt;     % Process the chosen position.
        set(LineHandle,'ydata',OutValues);% Visualize the change
    
    function[OUT]=ProcessIt()
        OUT=rand;  % this function will just return random value, for example.
    

    The DoIt function reads handle to radio buttons RB and determines which button is active. Then it change the appropriate value in Value variable and return it and changes y-values in line with handle L.

    In this example it will assign random value to the defined point but You can pass any variable from workspace / parent function and call any function.