How can I link a scroll-bar GUI with the mathematical code I have written?
5 views (last 30 days)
Show older comments
I am trying to write a program using GUIDE that has several scroll-bars. The program is going to plot a interference pattern of two point-sources of light varying certain variables using the scroll-bars. I have the math part of the code working and have some sort of the GUI as well. However, I cannot seem to get the two working together. The scroll-bars wont seem to vary the math part of the code.
If anybody could give me any advice I would greatly appreciate it.
1 Comment
Nicholas
on 19 Nov 2012
I'm not exactly sure what your question here is, however I can tell you that by default the sliders update only when the mouse button is released. A few weeks ago, though, I found some code on here that added "real time sliders". I can't find the exact link right now, but I have the code that I used.
function figure1_WindowButtonMotionFcn(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)
obj = get(YOURMAINFIGUREWINDOW, 'CurrentObject');
if ~isempty(obj)
if obj == YOURSLIDERHANDLE
set(YOURSLIDERHANDLE, 'Value', round(num2str(get(YOURSLIDERHANDLE, 'Value')) / 5)* 5 ) ); %%I was rounding the value here
set(SOMEDISPLAYHANDLE, 'String', num2str(get(YOURSLIDERHANDLE, 'Value'))); %%This line sets the handle value to an output
end
end
What this does is find which object your mouse is hovering over, and if it's a slider, it replaces a display value with that number. Instead of replacing a display value, you could perform a math function.
This should work, if not, I apologize as I'm not really a qualified expert.
Accepted Answer
Matt Fig
on 19 Nov 2012
Edited: Matt Fig
on 19 Nov 2012
Here is an example GUI that does something similar. You should be able to adapt it fairly easily...
function [S] = gui_scrollplot()
% Scroll through various Taylor approximations to exp(x)
S.fh = figure('units','pixels',...
'position',[500 500 320 460],...
'menubar','none',...
'name','gui_scrollplot',...
'numbertitle','off',...
'resize','off');
S.sl = uicontrol('style','slide',...
'unit','pix',...
'position',[290 10 20 440],...
'min',0,'max',8,...
'sliderstep',[1/8 1/8],...
'value',0);
S.V = 1; % The value of the slider.
S.x = -2:.01:2; % Make plot of exponential function.
S.ax = axes('units','pix',...
'pos',[30 30 250 400],...
'fontsize',8);
plot(S.x,exp(S.x))
hold on
S.L = plot(S.x,1,'r');
axis([-2 2 -1 9])
S.T = title('e^{x} and 1 term Taylor');
S.FUN = @(x,y) x.^y./factorial(y); % Taylor approx
set(S.sl,'callback',{@sl_call})
guidata(S.fh,S) % Save the handles structure.
if ~nargout
clear S
end
function [] = sl_call(varargin)
% Callback for slider.
S = guidata(gcbf);
V = get(S.sl,'value');
if ~V
D = ones(size(S.x));
else
D = sum(bsxfun(S.FUN,S.x,(0:V).'));
end
set(S.L,'xdata',S.x,'ydata',D);
set(S.T,'string',sprintf('e^{x} and %i term Taylor',V+1))
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks 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!