=polyfit function give error in matlab app designer app
8 次查看(过去 30 天)
显示 更早的评论
p = polyfit(x, y, 1); with arrays shown below.
When executed in the app, error is given :
"Error using polyfit (line 44)
X and Y vectors must be the same size."
When the same poly fit function is executed from the command window in matlab ( and putting the variables in workspace). the function works as expected.
For extra insight, the x and y variables are concatinated before using the poly function with the following code:
where xneg is columns 1 - 7(backwards) and yneg is 1-7 ( was originaly part of a larger array)
x = [flip(xneg); xpos];
y = [yneg(1:7) ypos];
Any help or insight would be greatly appriciated!
0 个评论
采纳的回答
Binaya
2024-8-20
Hi Collin
The error indicates that the values or sizes of "x" and "y" differ when used in App Designer. This issue arises because apps created with MATLAB App Designer do not use the base MATLAB workspace, which is why the same command may not throw an error in the command window.
To check the values of "x" and "y" in the App's workspace, add a breakpoint in the Code View of App Designer. When the code execution pauses at the breakpoint, you can access the App's workspace using the command window. You can refer to the given link to understand how to set a breakpoint in a MATLAB code: https://www.mathworks.com/help/matlab/matlab_prog/set-breakpoints.html.
更多回答(1 个)
Steven Lord
2024-8-20
Can you show us exactly how x and y are defined in your App Designer code? If you're retrieving them from a property of an edit box, are you using an EditField, a NumericEditField, or some other component? In particular what are the class of the x and y inputs with which you're calling polyfit?
x1 = [1 2 3];
class(x1)
x2 = '[1 2 3]';
class(x2)
y1 = [-2 -5 -9];
whos x1 y1 % same number of elements in x1 and y1
p1 = polyfit(x1, y1, 1) % works
y2 = '[-2 -5 -9]';
whos x2 y2 % different sizes because different number of characters
p1 = polyfit(x2, y2, 1) % does not work
If my suspicion is correct, that your x and y are text (char or string), you may want to switch to a NumericEditField.
另请参阅
类别
在 Help Center 和 File 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!