How to change a multi-variable matrix with different sliders?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Qu Cao
2016-8-24
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!