=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!

采纳的回答

Binaya
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 个评论
collin bannister
collin bannister 2024-8-20
Hello Binaya,
Thanks for this idea! turns out I had one array that needed to be transposed!
I need to use this tool more often !
The only down side is it will not report out app. variables. do you know if there is a way to force it to show the app. variable states as well as the local variables?
Thanks

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
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)
ans = 'double'
x2 = '[1 2 3]';
class(x2)
ans = 'char'
y1 = [-2 -5 -9];
whos x1 y1 % same number of elements in x1 and y1
Name Size Bytes Class Attributes x1 1x3 24 double y1 1x3 24 double
p1 = polyfit(x1, y1, 1) % works
p1 = 1x2
-3.5000 1.6667
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y2 = '[-2 -5 -9]';
whos x2 y2 % different sizes because different number of characters
Name Size Bytes Class Attributes x2 1x7 14 char y2 1x10 20 char
p1 = polyfit(x2, y2, 1) % does not work
Error using polyfit (line 49)
The first two inputs must have the same number of elements.
If my suspicion is correct, that your x and y are text (char or string), you may want to switch to a NumericEditField.
  1 个评论
collin bannister
collin bannister 2024-8-20
Hello Steven,
shown here is the variables, the x and y are brought in through an excel (.csv) document that are converted to double type arrays
Note I added a little bit of context in the original post to add info about array manipulation before the poly fit function.
Thanks for the help!

请先登录,再进行评论。

类别

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

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by