Info

This question is closed. Reopen it to edit or answer.

How do I create a plot with a slider for data generated using vectors?

1 view (last 30 days)
Hello,
I've written the following code that plots the height of water in a tank vs. time. I need to create a slider in the plot so that I can vary the parameter A_t and I want the plot to be updated after I release the mouse button.
h_init = 20;
t = 0:0.01:100;
t_0 = 0;
h = zeros(1, length(t));
myeps = 1e-7;
i = 1;
h(1) = h_init;
A_t = 16;
while h(i) > myeps
i = i + 1;
h(i) = (sqrt(h_init) - (0.18/A_t)*sqrt(981/2)*(t(i) - t_0))^2;
end
f = figure;
h1 = plot(t(1:i), h(1:i));
xlabel('Time (sec)')
ylabel('Height (cm)')
b = uicontrol('Parent',f,'Style','slider','Position',[81,54,419,23],...
'value',A_t, 'min',14, 'max',17);
bgcolor = f.Color;
set(b,'Callback',@(es,ed) updateSystem(h1,????????])));
In the while loop, 'i' is updated every time depending on the value of A_t because I want the while loop to terminate when h is almost zero. I'm using 'i' to extract data from the t and h vectors. My problem is, I cannot figure out how to implement the while loop and 'i' in the callback function.
Any help would be greatly appreciated.
KG

Answers (1)

Koustubh Gohad
Koustubh Gohad on 7 Nov 2014
I wrote a function to generate the h and t vectors.
function [h1, t1] = update_plot(A_t)
t = 0:0.01:100;
h = zeros(1, length(t));
h_init = 20;
t_0 = 0;
myeps = 1e-5;
i = 1;
h(1) = h_init;
while h(i) > myeps
i = i + 1;
h(i) = (sqrt(h_init) - (0.18/A_t)*sqrt(981/2)*(t(i) - t_0))^2;
h1 = h(1:i);
t1 = t(1:i);
end
And modified my code:
A_t = 16;
[h1, t1] = update_plot(A_t);
f = figure;
h = plot(t1, h1);
b = uicontrol('Parent',f,'Style','slider','Position',[81,54,419,23], 'value',A_t, 'min',14, 'max',17);
bgcolor = f.Color;
set(b,'Callback',@(es,ed) updateSystem(h,update_plot((es.Value))))
When I click on one of the slider arrows, I get an error. Also, is there a way I can include the function in my script file?

Community Treasure Hunt

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

Start Hunting!