Taking expression input and plotting it within an interval using app designer

조회 수: 4 (최근 30일)
I need to take a single variable expression as input from the user and give the plot for the same. I am able to take the expression input and make it a function using inline, but when I go for plotting it within an interval it shows the following error:
Error using inlineeval (line 14)
Error in inline expression ==> x^2 - 11*x + 30
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix powers, use '.^'.
Error in inline/subsref (line 23)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);
My code for input was:
s = app.EnterHereEditField.Value;
f = inline(s);
and for plotting:
x = -10:.1:10;
y = f(x);
plot(app.UIAxes,x,y);
I am new to matlab app designer, if there's some syntax error please tell. Thanks!

채택된 답변

Shanmukha Voggu
Shanmukha Voggu 2021년 9월 1일
편집: Shanmukha Voggu 2021년 9월 2일
Hi Aditya,
The error(Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a scalar. To perform elementwise matrix powers, use '.^'.) is regarding the expression(x^2 - 11*x + 30)
To understand the difference between x.^2 and x^2, let's create a square matrix x
x=[1,2;3,4]
ElementWiseSquare=x.^2 % Every element in the matrix is raised to power "2"
MatrixMultiplication=x^2 % For matrix multiplication, the number of columns in the first matrix must be equal to the number of rows in the second matrix
let's create a row matrix x
x=[1,2]
ElementWiseSquare=x.^2 % Every element in the matrix is raised to power "2"
MatrixMultiplication=x^2 % This statement gives an error because columns in [1,2](first matrix) not equal to rows in [1,2](second matrix)
There can be two fixes for the issue:
1) replace the x^2 in the expression(x^2 - 11*x + 30) with x.^2
2) or make sure x is a square matrix.
refer this for more information.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Control System Toolbox에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by