How to use callbacks to plot data from an existing matlab data file?
8 views (last 30 days)
Show older comments
Hello. I am having trouble grasping the way callbacks work to get my user interface code working properly. What I'm trying to do is this:
I have 4 sliders that select the indexes for picking up data from an already existing .m file (a 5D array), after which a for loop runs through the last dimension to get the y values for my x-y plot.
Right now the code does not work because the way my S.y function is defined isn't allowed - it should be a cell array, but I don't really think I need that.
All of what I need is pretty much coded, but the way my lines for S.y and y are coded won't allow the update functions to work properly.
function [] = slider_plot()
load wavelength.mat;
load grating_spacing.mat;
load diffraction_order.mat;
load angle_falling.mat;
load angle_dispersion_1_dgrating.mat;
% Plot different plots according to slider location.
% Define figure, normalized position can be adjusted to fit individual
% monitors.
S.fh = figure('units','normalized',...
'Position', [0.515 0.025 0.415 0.87]);
% Define axes so that room is available in figure window for sliders
S.ax = axes('unit','normalized',...
'position',[0.03 0.1 0.5 0.85]);
% General function
for l = 1:length(wavelength)
S.y(l) = @(par) angle_dispersion_1_dgrating(l, par(1), par(2), par(3), par(4));
end
% Define inital parameter values
S.a = 1; %angle falling
S.b = 1; %diffraction order
S.c = 1; %grating spacing
S.d = 1; %transmission or reflection, 1 - transmission
% x axes
S.x = wavelength;
hold on;
% Plot curve
S.p2 = plot(S.x,S.y);
hold off;
update(S);
% Slider for angle falling parameter:
S.aSlider = uicontrol('style','slider',...
'unit','normalized',...
'min',1,'max',length(angle_falling),'value', S.a,...
'position',[0.53 0.9 0.2 0.05],...
'sliderstep',[1 1],...
'callback', {@SliderCB, 'a'});
% Add a text uicontrol to label the slider.
txta = uicontrol('Style','text',...
'unit','normalized',...
'position',[0.2 0.11 0.7 0.02],...
'String','Angle of incidence, deg.');
% diffraction order Slider:
S.bSlider = uicontrol('style','slide',...
'unit','normalized',...
'min',1,'max',length(diffraction_order),'value', S.b,...
'sliderstep',[1 1],...
'callback', {@SliderCB, 'b'});
% Add a text uicontrol to label the slider.
txtb = uicontrol('Style','text',...
'unit','normalized',...
'String','Diffraction order');
% grating spacing Slider:
S.cSlider = uicontrol('style','slide',...
'unit','normalized',...
'min',1,'max',length(grating_spacing),'value', S.c,...
'sliderstep',[1 1],...
'callback', {@SliderCB, 'c'});
% Add a text uicontrol to label the slider.
txtc = uicontrol('Style','text',...
'unit','normalized',...
'String','Grating spacing, m');
% transmission or reflection, 1 - transmission Slider:
S.dSlider = uicontrol('style','slide',...
'unit','normalized',...
'min',1,'max',2,'value', S.d,...
'sliderstep',[1 1],...
'callback', {@SliderCB, 'd'});
% Add a text uicontrol to label the slider.
txtd = uicontrol('Style','text',...
'unit','normalized',...
'String','Transmission-reflection select');
guidata(S.fh, S); % Store S structure in the figure
end
% Callback for all sliders defined above
function SliderCB(aSlider, EventData, Param)
S = guidata(aSlider); % Get S structure from the figure
S.(Param) = get(aSlider, 'Value'); % Any of the 'a', 'b', etc. defined
update(S); % Update the plot values
guidata(aSlider, S); % Store modified S in figure
end
% Plot update function, creates new y-vector for plot and replaces the plot
% S.p2 with new y-vector
function update(S)
load wavelength.mat;
for l = 1:length(wavelength)
y(l) = S.y([S.a, S.b, S.c, S.d]);
end
set(S.p2, 'YData', y); % Replace old plot with new plotting values
end
2 Comments
Dennis
on 8 Aug 2018
How should y look like? I imagined a vector, but your code tries to write 4 values to each y(l), but always the same 4 values.
Accepted Answer
Stephen23
on 8 Aug 2018
Edited: Stephen23
on 8 Aug 2018
Function handles are scalar by definition: they cannot be formed into an array of function handles, as you are trying to do. To store multiple function handles you will need to put them into a cell array:
for kk = 1:numel(...)
S.y{kk}) = @(par) ...
end
and then when calling those functions:
for kk = 1:numel(S.y)
S.y{kk}(...)
end
4 Comments
Stephen23
on 8 Aug 2018
"The output in this case would be exclusively a scalar"
In that case there should be no problems with that allocation.
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!