MATLAB App Designer中正弦(Sin)按钮回调函数编写及计算功能实现求助
Hey there! Let's get that sine function up and running in your MATLAB App Designer calculator. I'll walk you through the key steps to make this work smoothly:
First, make sure your app has these core components set up:
- An input field (either
Edit Field (Numeric)orEdit Field (Text)): This is where users will enter values like45orSin(45). - A Sin button: The component that triggers the calculation when clicked.
- An output field (or display label): To show the calculated sine result.
The magic happens in the button's callback. Here are two common scenarios depending on how your users will input values:
Scenario 1: Users enter raw numbers (e.g., 45)
If your input field is numeric, use this callback to convert degrees to radians (since MATLAB's sin() function uses radians by default) and compute the result:
function SinButtonPushed(app, event) % Grab the input value from your numeric field (replace with your component's actual name) inputAngle = app.InputNumericField.Value; % Validate input to avoid errors if isnumeric(inputAngle) && ~isnan(inputAngle) % Convert degrees to radians radianAngle = deg2rad(inputAngle); % Calculate sine value sineResult = sin(radianAngle); % Display the result in your output field (replace with your component's name) app.OutputDisplayField.Value = round(sineResult, 4); % Round to 4 decimals for readability else app.OutputDisplayField.Value = "Invalid Input"; end end
Scenario 2: Users enter formatted input (e.g., Sin(45))
If users type the full Sin(...) string, use a text input field and regex to extract the numeric value inside the parentheses:
function SinButtonPushed(app, event) % Grab the input text from your text field inputText = app.InputTextField.Text; % Use regex to extract numbers from Sin(...) format valueMatches = regexp(inputText, 'Sin\((\d+(\.\d+)?)\)', 'tokens'); if ~isempty(valueMatches) % Convert extracted string to number inputAngle = str2double(valueMatches{1}{1}); if ~isnan(inputAngle) radianAngle = deg2rad(inputAngle); sineResult = sin(radianAngle); app.OutputDisplayField.Value = round(sineResult, 4); else app.OutputDisplayField.Value = "Invalid Input"; end else % Fallback: handle raw number inputs too inputAngle = str2double(inputText); if ~isnan(inputAngle) radianAngle = deg2rad(inputAngle); sineResult = sin(radianAngle); app.OutputDisplayField.Value = round(sineResult, 4); else app.OutputDisplayField.Value = "Invalid Input"; end end end
- Add a small label near the input field (e.g., "Enter angle in degrees") to clarify what kind of input you expect.
- Handle edge cases like negative angles or decimal values (both code snippets above already support these!).
- If you want to support radians instead, just remove the
deg2rad()conversion and update your label to say "Enter angle in radians".
Try these test cases to make sure everything works:
- Input
45→ Should return ~0.7071 - Input
Sin(30)→ Should return 0.5 - Input
-90→ Should return -1.0 - Input
abc→ Should show "Invalid Input"
内容的提问来源于stack exchange,提问作者Mohammad

