MATLAB Answers

How to start a script in App designer and update a variable with slider ?

77 views (last 30 days)
Raed Wasseghi
Raed Wasseghi on 7 Oct 2020
Answered: Nitin Kapgate on 3 Nov 2020
Hey everyone,
I have read through the forum, watched youtube videos and read the "help ...." explanations in matlab, but Im just getting more confused. I hope to find some helpful advices here.
First, I would like to run a script through appdesigner, so that the workspace is filled with the variables and I can use them, for instance with "evalin(...)" command. However, many comments suggest to use "run SCRIPTNAME" or run("SCRIPTNAME"), but it does no fill the workspace with the variables. I put this in the startupFcn(app), but it didn't show the results I need.
Is it possible? Or do you always have to run the script separately ? I also tried to do the other way around and let the script run my app designer program, but im not really satisfied with this approach.
Second, lets say the Script generates a random Matrix with M=randi([a 200], 5, 5) and I would like to adapt variable a with the slider. I am not sure how to do that, my current approach is in the button callback function:
function StartButtonPushed(app, event)
Matrix = evalin("base", "M");
Aslider = app.Slider.Value;
assignin("base", "a", "Aslider");
run myscript.m;
app.UITable.Data = Matrix;
This is what I got out of the many answers I read, but the slider doesn't work. I'm sure the slider needs to be programmed differently, but the examples from matlab dont show the connection with the variables from the script. Thanks in advance!

Answers (1)

Nitin Kapgate
Nitin Kapgate on 3 Nov 2020
It’s not recommended to use variables from base workspace in App Designer Apps as base workspace variables could get modified by other MATLAB scripts.
It is recommended to use properties in App Designer and create App’s own workspace. You can declare different properties (parameters/variables) that can be used by the app by adding private/public properties in the Code-View in App Designer. These properties can then be initialized in startupFcn and used/modified in different Callback functions. These properties can be used across the app to achieve the desired functionality. These properties can be referred with the syntax appName.PropertyName.
Refer to the MysliderSliderValueChanged slider callback function in the attached appSlider app that demonstrates the use of slider to change a parameter’s value. You can run the app and try changing the slider value.

Community Treasure Hunt

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

Start Hunting!