How to use callbacks to plot data from an existing matlab data file?

조회 수: 8 (최근 30일)
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
Dennis
Dennis 2018년 8월 8일
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.
Edvinas Gvozdiovas
Edvinas Gvozdiovas 2018년 8월 8일
편집: Edvinas Gvozdiovas 2018년 8월 8일
The x is wavelength, the y is
for l = 1:length(wavelength)
y(l) = angle_dispersion_1_dgrating(l, a, b, c, d);
end
It is just a simple 2D plot. a, b, c, d are the parameters that I'm supposed to adjust with sliders, which are just simple indexes in case I change up the data. I can view the actual values of those parameters using a textbox or legend if I want to. That's all there is to what I want from my code. (angle_dispersion_1_dgrating is a pre-supplied .m file with the y values I need)

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 8월 8일
편집: Stephen23 2018년 8월 8일
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
Edvinas Gvozdiovas
Edvinas Gvozdiovas 2018년 8월 8일
The output in this case would be exclusively a scalar. I should be able to work this out, thanks a lot!
Stephen23
Stephen23 2018년 8월 8일
"The output in this case would be exclusively a scalar"
In that case there should be no problems with that allocation.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by