MATLAB: Passing gui input variables to mfile to run but the input variables arent passing, how do i set them as handles to use in the mfile i want to run

guimatlab gui

Ive been using an mfile for a visual stimulation paradigm and i decided to create a gui to input variables to the mfile so the stimulation can run. The input variables however are not passing from the gui to the mfile when I run the paradigm.
% --- Executes on button press in StimulationVisual.
function StimulationVisual_Callback(hObject, eventdata, handles)
% hObject handle to StimulationVisual (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
f_stim_vis = str2double(get(handles.f_stim_vis,'String'));
totalTime_vis = str2double(get(handles.totalTime_vis,'String'));
r_interval_vis = str2double(get(handles.r_interval_vis,'String'));
s_interval_vis = str2double(get(handles.s_interval_vis,'String'));
set(handles.f_stim_vis,'String',f_stim_vis);
set(handles.totalTime_vis,'String',totalTime_vis);
set(handles.r_interval_vis,'String',r_interval_vis);
set(handles.s_interval_vis,'String',s_interval_vis);
save('Visual_Stimulus','f_stim_vis','totalTime_vis','r_interval_vis','s_interval_vis');
run Visual_Stimulus

Best Answer

Your code does not load() from Visual_Stimulus.mat or from anywhere else. Instead it has
totalTime_vis = 20; %total recording time in seconds

r_interval_vis = 5; %resting block in seconds

s_interval_vis = 5 ; %stimulation block in seconds

which assigns constant values to those parameters.
You should rewrite as a function that accepts those as parameters. Change
save('Visual_Stimulus','f_stim_vis','totalTime_vis','r_interval_vis','s_interval_vis');
run Visual_Stimulus
to
Visual_Stimulus(f_stim_vis, totalTime_vis, r_interval_vis, s_interval_vis)
and at the very top of Visual_Stimulus.m put in
function Visual_Stimulus(f_stim_vis, totalTime_vis, r_interval_vis, s_interval_vis)
if ~exist('f_stim_vis', 'var')
f_stim_vis = 1;
end
if ~exist('totalTime_vis', 'var')
totalTime_vis = 20;
end
if ~exist('r_interval_vis', 'var')
r_interval_vis = 5;
end
if ~exist('s_interval_vis', 'var')
s_interval_vis = 5;
end
and delete the lines
f_stim_vis=1; %alternating frequency in Hz
and
totalTime_vis = 20; %total recording time in seconds
r_interval_vis = 5; %resting block in seconds
s_interval_vis = 5 ; %stimulation block in seconds
With these changes, the code would continue to do what it already does if the user did not pass anything to Visual_Stimulus, but if someone does pass parameters then the parameters would override the default values.
Related Question