sofia
sofia

Reputation: 1

Matlab: How to pass a variable from an edit text box in a GUI to another m file?

I am creating a MATLAB GUI using GUIDE. The GUI has radio buttons, pop-up menus, an editable text box, and a push-button. I am fine with working with the radio buttons and pop-up menus. However, I want to take the value from the editable text box (that the user enters) and assign it to a variable nu. Then, I want to use this value of nu in another m-file that I call from the GUI upon the user pushing the push-button, so that necessary calculations are made (the code for the calculations are in the m-file).

The problem arises when I try passing the value of nu from the GUI (that the user entered) into the m-file.

The code for the editable text box callback is given as follows:

function etxt_freq_Callback(hObject, eventdata, handles)

% hObject    handle to etxt_freq (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 etxt_freq as text
%        str2double(get(hObject,'String')) returns contents of etxt_freq as a double

% input = str2double(get(hObject,'string'));
% if isnan(input)
%  errordlg('You must enter a numeric value between 0 and 1','Invalid Input','modal')
%  uicontrol(hObject)
%  return
% else
%  display(input);
% end
% handles.nu = str2num(get(hObject, 'string'))
% guidata( hObject, handles );

nu = str2num(get(hObject, 'string'))

Any suggestions would be great!

Thanks in advance for your help.

Upvotes: 0

Views: 9027

Answers (1)

Seongbong Kim
Seongbong Kim

Reputation: 11

I think that you need to write a code in a pushbutton Callback function, if you want to operate external m-file function when you push the button.

like ..

nu = str2num(get(handles.etxt_freq, 'String'));

And I think it can be a solution that make a function with input argument.
Below is my example, and it works for me.

I made a function like below, and place it at same directory of my GUI files(.fig/.m)

function [ double_nu ] = make_value_double( nu )
% make_value_double function get a value from argument.
% And return a value that two times of nu.

    double_nu = nu * 2;

end

And the GUI code

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

nu = str2num(get(handles.etxt_freq, 'String'));
nu1 = make_value_double(nu);
set(handles.text1, 'String', nu1);

I made static text box to check whether function works or not.
When I push the button,
1. 'pushbutton1' get a 'String' value and make it to number.
2. This number assigned to 'nu'.
3. 'nu' is become an argument of my function. (make_value_double)
4. My function does calculation on external file. (make_value_double.m)
5. Assign calculated value to nu1, and print it to static text box.(handles.text1)

------------------------------------------------------------------
Or how about using save & load function?
Make 'nu' to 'nu.mat' and load 'nu.mat' in another program.

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

nu = str2num(get(handles.etxt_freq, 'String'));

% save(filename, variable)
save('nu.mat', 'nu');

In your simulink code, type load('nu.mat'); command will make variable 'nu' to workspace of your simulink code. Then use it.

Optionally, you could make CloseRequestFcn to delete 'nu.mat'. By doing this, When you close the figure/GUI, 'nu.mat' also deleted. Then, your simulink function cannot use 'nu.mat' while the GUI is off.

% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(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)

% Hint: delete(hObject) closes the figure
delete(hObject);

% delete 'nu.mat' in your working directory
delete 'nu.mat';

Upvotes: 1

Related Questions