MATLAB: How to pass values selected from a GUI interface to another *.m script

MATLABuicontrol

I am trying to create a GUI interface like the below: a popup menu to select the depth, a slide bar to select the lower end of the colorbar, and then a button to execute the program. My question is how do I pass the values of the two selections (depth and slidebar values) to the matlab program this GUI interface is linked to?
Many thanks.
h1 = uicontrol('Style', 'popup',...
'String', ['| 0 | 100 | 200 |'], 'FontSize', 12, 'Position', [150 240 200 80]);
h2 = uicontrol('style','slide','min',7.0,'max',8.0,'val',7.5
'position',[20 10 260 30]);
h3 = uicontrol('Style', 'pushbutton', 'String', 'plot the figure',...
'FontSize', 14,'Position', [150 45 200 40], 'Callback', 'Calculation');

Best Answer

Leon - what is the signature of the function (program) that your GUI is "linked" to? Why not just update the signature of this function so that when you call it, you pass in the depth and slider values into it? Perhaps something like the following will be useful
function myGui
h1 = uicontrol('Style', 'popup',...
'String', {'0','100','200'}, 'FontSize', 12, 'Position', [150 240 200 80],'Value',1);
h2 = uicontrol('style','slider','min',7.0,'max',8.0,'val',7.5, ...
'position',[20 10 260 30]);
h3 = uicontrol('Style', 'pushbutton', 'String', 'plot the figure',...
'FontSize', 14,'Position', [150 45 200 40], 'Callback', @Calculation);
function Calculation(source,eventdata)
% get the depth
depthIdx = get(h1,'Value');
depthStrings = get(h1,'String');
depth = str2double(depthStrings{depthIdx});
% get the slider value
sliderValue = get(h2,'Value');
% call your function
doCalculation(depth,sliderValue);
end
end
Note how the Calculation callback is nested within the parent myGui function so that it can access its local variable...namely the handles to the controls. Because of this, we can access the depth and slider value and then pass them into the function that will perform the calculation.