Placing a slider control on a surf plot?

14 views (last 30 days)
Is there a way to implement a slider-type control for a 3D surf plot? I've looked into uicontrol but it apparently only works for plots created with figure.
Is there a different way of implementing this? The control doesn't have to be a slider; I want something that will enable the user to input a frequency value within 20Hz-20kHz, which then goes through a section of code that does the math with this value and updates the surf plot accordingly.
Sample code that I want to put a slider on is below. Attempting to use uicontrol gives me the error: surface cannot be a parent.
% Plotting a sphere who's radius changes based on slider position on plot
% Defining theta, phi and r for spherical coordinates
theta = linspace(-pi,pi,73);
theta = repmat(theta,37,1);
theta(:,1) = -1.*theta(:,1); % not necessary but it connects the last set of points with the first to give a smooth surface
phi = linspace(-pi/2,pi/2,37)';
phi = repmat(phi,1,73);
r = user_input*ones(size(theta)); % user_input comes from the slider that changes the radius of plotted sphere
[x,y,z] = sph2cart(theta,phi,r);
surf(x,y,z)

Accepted Answer

Walter Roberson
Walter Roberson on 22 Sep 2021
You are using surf() and you are not passing in an axes handle to surf().
In that combination, surf() is going to use gca() to find the current axes, and gca() is going to use gcf(), which will find the current "traditional" figure (or create one if needed) . gcf() will never try to find a "current" uifigure()
So, with the way you have programmed, your surf() is always going to be displayed in a traditional figure(). So your concern about uicontrol only working with plots created with figure() turns out not to be a problem: you are going to be using a plot created with figure()
If you had programmed all of this into a uifigure() then instead of uicontrol() you would use uislider()
  5 Comments
Walter Roberson
Walter Roberson on 22 Sep 2021
Put the code starting from ax = app.ax1; inside of a function that accepts two parameters: a handle to the slider, and an event structure .
When you create the uislider(), set its Position (location on the screen) and its Limits, and its initial Value, and set its ValueChangedFcn property to be a handle to the function mentioned above. After having done that, call the function "manually" to draw the initial sphere.
Karan Gundre
Karan Gundre on 22 Sep 2021
I thought it needed more work. I know where to look to get all of that dialed in. Thank you for your help! Your answers on several other posts have helped me immensely in the past. Thank you for all of that as well. MVP indeed. Have a good one!

Sign in to comment.

More Answers (0)

Categories

Find more on Develop uifigure-Based Apps in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!