How to modify SliderGUI block behavior with spring action falling to zero when released ?

43 views (last 30 days)
I am using Appdesigner with MATLAB 2017B. This slider UI component I want to modify like this.
1. It should have initial value 0 .
2.When moved by mouse click the increment will be as per mouse pointer movement.
3.When I release the slider it should fall back to zero.
Technically it is block to be used to give "Input" but my requirement is to "overwrite" its value to zero when released.
I also need that fall back to zero also needs to be in controlled manner ..like decerement by 5 per sample.
e.g. Run the app slider value is 0. Move the slider slowly to 100 , relase it..it will move down to zero with count of 5 per sample.
I will be passing the value of slider to Constant in Simulink Model continuously.
Any suggessions ?

Accepted Answer

AK
AK on 29 Oct 2024 at 12:17
Thanks for very clear directions.
It worked as expected.

More Answers (1)

Abhas
Abhas on 29 Oct 2024 at 5:53
Hi @AK,
To achieve the functionality you described using MATLAB App Designer in R2017b, you will need to set up a slider UI component and implement a callback function to handle the slider's behavior. Here's a step-by-step guide:
  • Open MATLAB App Designer: Start MATLAB and type "appdesigner" in the command window to open the App Designer.
  • Create a New App: Click on "New" to create a new app.
  • Add a Slider Component: Drag a Slider from the Component Library to the Design View.
  • Set ValueChangingFcn Callback: App Designer will create a new function "ValueChangingFcn" in the Code View. This function will be called whenever the slider is moved.
function SliderValueChanging(app, event)
newValue = event.Value;
% Here you can pass newValue to your Simulink model
disp(['Slider value changing: ', num2str(newValue)]);
end
  • Set ValueChangedFcn Callback: Implement logic to decrement the slider value back to zero at a controlled rate.
% Callback function: Slider
function SliderValueChanged(app, event)
currentValue = event.Value;
disp(['Slider value released at: ', num2str(currentValue)]);
% Decrement the slider value to zero in steps of 5
while currentValue > 0
pause(0.1); % Adjust this to control the speed of decrement
currentValue = currentValue - 5;
currentValue = max(0, currentValue); % Ensure it doesn't go below zero
app.Slider.Value = currentValue;
% Here you can pass currentValue to your Simulink model
disp(['Slider value decrementing: ', num2str(currentValue)]);
% Exit the loop if currentValue is zero
if currentValue == 0
break;
end
end
end
This setup allows you to have a slider that resets to zero in a controlled manner after being released, with the decrement happening in steps of 5.
You may refer to the below MathWorks documentation links to know more about setting the callbacks and configuring the slider:
  1. https://www.mathworks.com/help/matlab/ref/matlab.ui.control.slider.html
  2. https://www.mathworks.com/help/matlab/creating_guis/write-callbacks-for-gui-in-app-designer.html

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!