Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit:

How can I link a scroll-bar GUI with the mathematical code I have written?

Asked by Caleb Pocock on 19 Nov 2012

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)
           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

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.

Caleb Pocock


No products are associated with this question.

1 Answer

Answer by Matt Fig on 19 Nov 2012
Edited by Matt Fig on 19 Nov 2012
Accepted answer

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],...
              'resize','off'); = uicontrol('style','slide',...
                 'position',[290 10 20 440],...
                 'sliderstep',[1/8 1/8],...
S.V = 1;  % The value of the slider.           
S.x = -2:.01:2;  % Make plot of exponential function. = axes('units','pix',...
            'pos',[30 30 250 400],...
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
guidata(S.fh,S)  % Save the handles structure.
if ~nargout
    clear S
function [] = sl_call(varargin)
% Callback for slider.
S = guidata(gcbf);
V = get(,'value');
if ~V
    D = ones(size(S.x));
    D = sum(bsxfun(S.FUN,S.x,(0:V).'));
set(S.T,'string',sprintf('e^{x} and %i term Taylor',V+1))

1 Comment

Caleb Pocock on 19 Nov 2012

Here is the code that I have written so far. i was able to get one of the scroll-bars to change the data I want changed. However, I cant seem to get any more to work with the other data.

function ex_uicontrol
    % Example code for uicontrol reference page
    % Create a figure and an axes to contain a 3-D surface plot.
    hax = axes('Units','pixels');
      % Add a slider uicontrol to control the vertical scaling of the
      % surface object. Position it under the Clear button.
      uicontrol('Style', 'slider',...
          'Position', [400 20 120 20],...
          'Callback', {@surfzlim,hax}); 
  					% Slider function handle callback
            % Implemented as a local function
function surfzlim(hObj,event,ax) %#ok<INUSL>
% Variables
% Angles in radians
d = 50^-3; % separation
ss = .1; % screen size
a = 0; % alpha angle
b = 0; % beta angle
g = 0; % gamma angle
ra = [cos(a) sin(a) 0; -sin(a) cos(a) 0; 0 0 1]; % alpha rotation matrix
rb = [1 0 0; 0 cos(b) sin(b); 0 -sin(b) cos(b)]; % beta rotation matrix
rg = [cos(g) sin(g) 0; -sin(g) cos(g) 0; 0 0 1]; % gamma rotation matrix
rotation = ra*rb*rg; % total rotation matrix
source1 = [0 d/2  0]*rotation; % source1 vector
source2 = [0 (-d/2) 0]*rotation; % source2 vector
sdim = linspace(-ss/2, ss/2, 1000); % screen array
%%%%%%%%%%%%%START%%%%%%%%%%%%%%%%% Added 111512 TDM
[sdimX sdimY]   = meshgrid(sdim, sdim);
S = .1; % distance to screen
wavelength = get(hObj,'Value'); % wavelength of light
opd = zeros(1000, 1000); % optical path difference
l = 1;
k = 1;
screen = zeros(length(sdim), 1, length(sdim));
%%%%%%%%%%%%%START%%%%%%%%%%%%%%%%% Added 111512 TDM
%try this
OPL1    = ((source1(1)-sdimX).^2 + (source1(2)-sdimY).^2 + (source1(3)-S).^2).^(0.5);
OPL2    = ((source2(1)-sdimX).^2 + (source2(2)-sdimY).^2 + (source2(3)-S).^2).^(0.5);
opd     = OPL1 - OPL2;
% while l <= length(sdim)
%     while k <= length(sdim)
%         opd(l, k) = abs(sqrt((sdim(l) - source1(1))^2 + (S - source1(2))^2 + (sdim(k) - source1(3))^2) - sqrt((sdim(l) - source2(1))^2 + (S - source2(2))^2 + (sdim(k) - source2(3))^2));
%         k = k + 1;
%     end
%     k = 1;
%     l = l + 1;
% end
n = opd/wavelength - floor(opd/wavelength);
I = cos(2*pi*n) + 1;
%%%%%%%%%%%%%START%%%%%%%%%%%%%%%%% Added 111512 TDM
figure(10);imagesc(sdim,sdim,I);colormap(gray);axis image

So looking at this I was able to vary wavelength with the scroll-bar. I would like to add several more to be able to vary d, ss, a, b, g, and S.

Matt Fig

Contact us