Create a GUI to change variables in a script

5 views (last 30 days)
Ryan
Ryan on 12 Mar 2024
Answered: Ishaan Mehta on 26 Dec 2024
I'm trying to create a GUI to change variables in a script based on user selection.
For example I have a j function and a line reads as:
j.fastener = Bolt(bolt_size, bolt_material, bolt_length);
but I want to create a GUI that will change
bolt_size
bolt_material
bolt length
based on what the user selects
so say the user selects the above options,
then based on these inputs, the variables in the script should change to
j.fastener = Bolt('10-32', 'A286', .125);
So I am trying to get inputs from a use to change the variables of the script itself based on the selection.
  6 Comments
John D'Errico
John D'Errico on 14 Mar 2024
Edited: John D'Errico on 14 Mar 2024
Learn to use functions. Then you can pass in any variables you want. Your code need never change on the fly, a terribly bad idea in general.
As far as how to change a variable contents based on what you pass in, that part is trivial. It is the essence of what a function does!
myfun('First time called')
b = 'First time called'
myfun('Second time called')
b = 'Second time called'
function myfun(b)
b
end
Do you see that b takes on the value you pass in?
Stephen23
Stephen23 on 14 Mar 2024
Just call a function directly from the GUI. Avoid scripts.

Sign in to comment.

Answers (1)

Ishaan Mehta
Ishaan Mehta on 26 Dec 2024
Hi Ryan,
You can create a MATLAB function that thakes the 3 inputs, namely, bolt_size, bolt_material, and bolt length, and assign its result to "j.fastener".
A MATLAB function is a defined block of code encapsulated within a separate .m file, designed to perform a specific computational task. It consists of a function signature that specifies the function's name, input arguments, and output arguments. The function body contains executable statements that implement the desired operations using the input parameters to produce the outputs.
Learn more about creating MATLAB functions here: https://www.mathworks.com/help/matlab/ref/function.html
As an alternative to creating an application using MATLAB App Designer, you can create a simple MATLAB live script that takes in input values as dropdowns within the script itself, and then uses the selected values in the code that follows, as descibed in the below documentation page:
It would ideally look similar to the image below:
Hope this helps!

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!