How to run a function within another function with specified input?

1 Ansicht (letzte 30 Tage)
Tim K
Tim K am 10 Okt. 2022
Kommentiert: Tim K am 11 Okt. 2022
Hi,
As you can see in the script below i made a figure with GUIDE and want to run the functions "S1.m" or "S2.m" if x3 or x4 = 1. How can i declare Hx,Wx,scalex and pat as input for S1.m or S2.m so this functions can interact with the values from Hx,Wx,scalex and pat?
Thanks!
function [varargout] = selection(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @selection_OpeningFcn, ...
'gui_OutputFcn', @selection_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
function selection_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = selection_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function cbE_Callback(hObject, eventdata, handles)
Single = get(handles.cbE,'Value');
switch Single
case 1
set(handles.cbS2,'Enable','On')
set(handles.cbN2,'Enable','On')
case 0
set(handles.cbS2,'Enable','Off')
set(handles.cbN2,'Enable','Off')
end
function cbS2_Callback(hObject, eventdata, handles)
function cbN2_Callback(hObject, eventdata, handles)
function bOK_Callback(hObject, eventdata, handles)
x3 = get(handles.cbS2,'Value');
x4 = get(handles.cbN2,'Value');
Hx = str2double(get(handles.tH,'String'));
scalex = str2double(get(handles.ts,'String'));
pat = get(handles.tpath,'String');
Wx = str2double(get(handles.tW,'String'));
if x3 == 1
run('S1.m')
end
if x4 == 1
run('S2.m')
end
function tW_Callback(hObject, eventdata, handles)
function tW_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function tH_Callback(hObject, eventdata, handles)
function tH_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function ts_Callback(hObject, eventdata, handles)
function ts_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function tpath_Callback(hObject, eventdata, handles)
function tpath_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

Akzeptierte Antwort

Jan
Jan am 10 Okt. 2022
Bearbeitet: Jan am 10 Okt. 2022
Convert the scripts S1 and S2 to functions, which accept these inputs. Then:
x3 = get(handles.cbS2,'Value');
x4 = get(handles.cbN2,'Value');
Hx = str2double(get(handles.tH,'String'));
scalex = str2double(get(handles.ts,'String'));
pat = get(handles.tpath,'String');
Wx = str2double(get(handles.tW,'String'));
if x3 == 1
S1(Hx, scalex, pat, Wx);
end
if x4 == 1
S2(Hx, scalex, pat, Wx);
end
You have posted a "function", not a "script". The difference is important. See: https://www.mathworks.com/help/matlab/matlab_prog/scripts-and-functions.html
Prefer functions for serious work, because they keep the work space clean.

Weitere Antworten (0)

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by