Create a GUI to change variables in a script

조회 수: 5 (최근 30일)
Ryan
Ryan 2024년 3월 12일
답변: Ishaan Mehta 2024년 12월 26일
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