Create a GUI to change variables in a script

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

the first attempt of this is I just want the user inputs to reflect in the script. If this is successful then I can try to take it a step futher and have it run the script after the inputs are made. But for now I am just trying to at least modify the variables based on what the user selects from the GUI.
Just run the script from the GUI. Don't make your code more complex than that.
Even better: avoid scripts and call a function from the GUI.
That was my original thinking, thanks for the input.
What would be an example of the code for say a drop down, and the options are A286 and 300 CRES.
If the user selects the option that says 300 CRES, then the variable b in the script would change to (a string in this case) as '300 CRES'. Can I do this?
There are certain instances where in the script needs strings stuch as 'name' and other that need numerical inputs.
So I'm just trying to have the user identify all of these upfront in a GUI and then based on selections or inputs (some will be blank enter fields) and then based on everything the script will populate with these selections accordingly so it can run.
Current process is manually typing out all of these within the script before running. I hope this makes sense, Thanks for your help, I am still relatively new to Matlab.
You may want to consider livescript with controls.
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?
Just call a function directly from the GUI. Avoid scripts.

Sign in to comment.

Answers (1)

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

Asked:

on 12 Mar 2024

Answered:

on 26 Dec 2024

Community Treasure Hunt

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

Start Hunting!