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

1 visualización (últimos 30 días)
Ulf Molich
Ulf Molich el 19 de Ag. de 2016
Respondida: Qu Cao el 24 de Ag. de 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.

Respuestas (1)

Qu Cao
Qu Cao el 24 de Ag. de 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':

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by