=polyfit function give error in matlab app designer app
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
collin bannister
le 20 Août 2024
Commenté : collin bannister
le 20 Août 2024
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 commentaires
Réponse acceptée
Binaya
le 20 Août 2024
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.
Plus de réponses (1)
Steven Lord
le 20 Août 2024
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.
Voir également
Catégories
En savoir plus sur Data Type Identification dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
