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

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

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.

*No products are associated with this question.*

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],... '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))

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. figure(10) hax = axes('Units','pixels'); imagesc;

% Add a slider uicontrol to control the vertical scaling of the % surface object. Position it under the Clear button. uicontrol('Style', 'slider',... 'Min',1E-9,'Max',1000E-9,'Value',500E-9,... 'Position', [400 20 120 20],... 'Callback', {@surfzlim,hax}); % Slider function handle callback % Implemented as a local function end

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); %%%%%%%%%%%%%%END%%%%%%%%%%%%%%%% 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; %%%%%%%%%%%%%%END%%%%%%%%%%%%%%%%

% 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; %meshc(I); %%%%%%%%%%%%%START%%%%%%%%%%%%%%%%% Added 111512 TDM figure(10);imagesc(sdim,sdim,I);colormap(gray);axis image %%%%%%%%%%%%%%END%%%%%%%%%%%%%%%% end

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.

## 1 Comment

## Nicholas

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/54156#comment_112029

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.

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.