Slider Movement Updates Plot

4 views (last 30 days)
Jose Treviño
Jose Treviño on 3 Dec 2015
Commented: Adam on 3 Dec 2015
Hi, I have been working in a school project that involves manipulating certain variables to obtain plot at specific values for each variable. I was able to do so using sliders for each variable and a push button on which the function is. This way I move sliders to desired positions then push "calculate" and I obtain the plots. However, I would like to whenever I move the slider, the function and the plot updates automatically. Without using the push button. I heard that adding a listener makes what I ask possible. However I'm pretty new to matlab and honestly I do not have any idea.
This is the code I have:
function varargout = FreeConvectionProblem(varargin)
% FREECONVECTIONPROBLEM MATLAB code for FreeConvectionProblem.fig
% Edit the above text to modify the response to help FreeConvectionProblem
% Last Modified by GUIDE v2.5 02-Dec-2015 09:22:18
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @FreeConvectionProblem_OpeningFcn, ...
'gui_OutputFcn', @FreeConvectionProblem_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 FreeConvectionProblem is made visible.
function FreeConvectionProblem_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% Choose default command line output for FreeConvectionProblem
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = FreeConvectionProblem_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
pan1 = uipanel('Units','normal', 'Position', [0 0 3/4 1]);
sliderVal1=get(handles.Prslider,'Value');
Pr = [sliderVal1]; % Choose Prandtl
sliderVal2=get(handles.NusselSlider,'Value');
etaMax = [sliderVal2]; % Choose etaMax
sliderVal3=get(handles.lengthslider,'Value');
xm = [sliderVal3];
sliderVal4=get(handles.yslider,'Value');
ym = [sliderVal4];
solinit = bvpinit(linspace(0, etaMax, 5), [0, 0, 0, 0, 0]);
sol = bvp4c(@NatConv, @NatConvBC, solinit, [], Pr);
eta = linspace(0, etaMax, 300);
y = deval(sol, eta);
ax1= subplot(2, 1, 1, 'Parent', pan1);
plot(eta, y(1,:), '-.k', eta, y(2,:), '-k', eta, y(3,:), '--k')
legend(ax1,'Stream function, f = y_1', 'Velocity, df/d\eta = y_2', ...
'Shear, d^2f/d\eta^2 = y_3')
axis([0 xm -0.2 ym])
xlabel(ax1,'\eta')
ylabel(ax1,'y_1, y_2, y_3')
ax2= subplot(2, 1, 2);
plot(ax2, eta, y(4,:), '-k', eta, y(5,:), '--k')
legend(ax2,'Temperature, T^* = y_4', 'Heat flux, dT^*/d\eta = y_5')
axis(ax2,[0 xm -1.2 1])
xlabel(ax2,'\eta')
ylabel(ax2,'y_4, y_5')
% --- Executes on slider movement.
function Prslider_Callback(hObject, eventdata, handles)
sliderVal1 = get(hObject,'Value'); %Recalling Slider Value
assignin('base','sliderVal1',sliderVal1) %Assigning Value to variable sliderVal
%set(handles.text6,'String',num2str(sliderVal1)); %Value output in textbox as String
sliderVal = get(hObject,'Value'); %Recalling Slider Value
assignin('base','sliderVal',sliderVal) %Assigning Value to variable sliderVal
set(handles.prval,'String',num2str(sliderVal)); %Value output in textbox as String
val = get(hObject,'Value'); %Convert from string to number
set(handles.prval,'String',num2str(val));
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function Prslider_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function NusselSlider_Callback(hObject, eventdata, handles)
sliderVal2 = get(hObject,'Value'); %Recalling Slider Value
assignin('base','sliderVal2',sliderVal2) %Assigning Value to variable sliderVal
set(handles.nuval,'String',num2str(sliderVal2)); %Value output in textbox as String
nuval = get(hObject,'Value'); %Convert from string to number
set(handles.nuval,'String',num2str(nuval));
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function NusselSlider_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes on slider movement.
function lengthslider_Callback(hObject, eventdata, handles)
sliderVal3 = get(hObject,'Value'); %Recalling Slider Value
assignin('base','sliderVal3',sliderVal3) %Assigning Value to variable sliderVal
set(handles.leval,'String',num2str(sliderVal3)); %Value output in textbox as String
leval = get(hObject,'Value'); %Convert from string to number
set(handles.leval,'String',num2str(leval));
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function lengthslider_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
% --- Executes during object creation, after setting all properties.
function Prval_CreateFcn(hObject, eventdata, handles)
% --- Executes on slider movement.
function yslider_Callback(hObject, eventdata, handles)
sliderVal4 = get(hObject,'Value'); %Recalling Slider Value
assignin('base','sliderVal4',sliderVal4) %Assigning Value to variable sliderVal
set(handles.yval,'String',num2str(sliderVal4)); %Value output in textbox as String
% --- Executes during object creation, after setting all properties.
function yslider_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
Thank you very much for your time.
  1 Comment
Adam
Adam on 3 Dec 2015
You should just be able to put the code from your pushbutton callback into the callback for the respective slider and that should do precisely what you want.
You should never be using
assignin( 'base',... )
in a GUI though. It doesn't make any sense to be sending variables outside of the GUI.

Sign in to comment.

Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!