MATLAB Answers

How to change a multi-variable matrix with different sliders?

6 views (last 30 days)
Ulf Molich
Ulf Molich on 19 Aug 2016
Answered: Qu Cao on 24 Aug 2016
Hi there, thanks for taking your time to read my question.
I have a figure with a plot and 8 sliders. The plot is calculated in a bit tricky way, so first I will present the code, then try to explain the basic idea:
% Calculate Y-contribution
for i=1:length(PValue);
% Calculate quadratic coefficients
intp_a = 2*invalues + 2*PValue{i} - 4*PHalfValue{i};
intp_b = -3*invalues - PValue{i} + 4*PHalfValue{i};
TotalSpectrum(:,i) = intp_a*Slider(i)^2+intp_b*Slider(i)+invalues;
end
% Designing of plot window
f=figure('Position',[400 300 1024 768]);
ax = axes('Parent',f,'position',[0.13 0.39 0.77 0.54]);
graph=plot(ax,Xvalues,sum(TotalSpectrum,2));
So, the plot is basically a a graph of data points, "Xvalues" and the y-values are calculated as a sum of 8 functions that depends on the position of their respective sliders.
Now, I've read for hours on this forum, and found a lot of useful information, but I'm not quite there yet. My problem is that all sliders change the same function. I believe this is because I have a variable amount of sliders, and because of this have the slider handles in an array:
for i=1:length(Slider)
sliderhandle(i)=uicontrol('Style', 'slider','Userdata',TotalSpectrum,'Tag',num2str(i));
addlistener(sliderhandle(i),'ContinuousValueChange',@(hObject, event) ...
updateSlider(hObject, event,graph,invalues,intp_a,intp_b));
end
I have removed the position to simplify the code here.
My updateSlider function looks like this:
function updateSlider(hObject,event,graph,invalues,intp_a,intp_b)
i = str2num(get(hObject,'Tag'));
Slider(i) = get(hObject,'Value');
TotalSpectrum = get(hObject,'Userdata');
TotalSpectrum(:,i) = intp_a*Slider(i)^2+intp_b*Slider(i)+invalues;
set(graph,'ydata',sum(TotalSpectrum,2));
drawnow;
end
Now, this works, but the problem is that all sliders does the same. Is this because the listener overwrites the previous listeners? Or some other explanation?
Thanks for reading till the very end.

  0 Comments

Sign in to comment.

Answers (1)

Qu Cao
Qu Cao on 24 Aug 2016
It might be difficult to understand your workflow without a snapshot of the GUI you are designing.
One suggestion to debug the code is to check the value of 'TotalSpectrum' in each iteration since it determines the positons of the sliders.
Please also refer to the following documentation for more information about 'addlistener':

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by