Create a GUI to change variables in a script

5 次查看(过去 30 天)
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 个评论
John D'Errico
John D'Errico 2024-3-14
编辑:John D'Errico 2024-3-14
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 2024-3-14
Just call a function directly from the GUI. Avoid scripts.

请先登录,再进行评论。

回答(1 个)

Ishaan Mehta
Ishaan Mehta 2024-12-26
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!

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by