I was trying to add errors messages if theres error in user input but I ended up with two errors that I couldn't figure it out how to fix
2 次查看(过去 30 天)
显示 更早的评论
function CalculerButtonPushed(app, event)
x=app.ValeurdeXEditField.Value;
a=str2num(x);
y=app.fxEditField.Value;
b=str2num(y);
z=app.fdeX.Value;
c=str2num(z);
if (app.ValeurdeXEditField.Value==' ')||(app.fxEditField.Value==' ')||(app.fdeX.Value==' ')
errordlg("Les vecteurs doivent ",'ERREUR');
else
if size(a)== size(y)
n=length(a);
p0=0;
for i=1:n
L=1;
for j=1:n
if i~=j
L=L.*(c-a(j))/(a(i)-a(j));
end
end
p0=p0+(b(i).*L);
end
Y=p0;
app.ResultatdefxTextArea.Value=num2str(Y);
else
errordlg("Les vecteurs doivent etre de la meme taille ",'ERREUR');
end
end
this is the errors i get:
1)
Error in app2/CalculerButtonPushed (line 37)
if (app.ValeurdeXEditField.Value==' ')||(app.fxEditField.Value==' ')||(app.fdeX.Value==' ')
Error while evaluating Button PrivateButtonPushedFcn.
2)
Index exceeds the number of array elements even if i type the same array size
1 个评论
回答(1 个)
prabhat kumar sharma
2023-10-5
编辑:prabhat kumar sharma
2023-10-5
I understand you are facing issues in the button pushed call back function.
1. The error received in the line 37 is likely due to the syntax you are using. You can correct it by using the “isequal” function as follows:
if isequal(app.ValeurdeXEditField.Value, ' ')|| isequal(app.fxEditField.Value, ' ') ||isequal(app.fdeX.Value, ' ')
errordlg("Les vecteurs doivent ",'ERREUR');
The “isequal” function is used to convert the operands to a logical scalar vector. For more information about the same, you can refer to the following documentation link:
2. On executing the provided code, I found another issue where you are trying to compare the sizes of “a” and “y”. Since “a” is a numeric array and “y” is a string array, the sizes will always be different and thus trigger the error dialogue. It can be rectified by comparing the sizes of variables “a” and “b”.
Making the above-mentioned changes resulted in smooth execution of the application.
I hope this helps!
Regards,
Prabhat Sharma
1 个评论
Walter Roberson
2023-10-5
I wonder if you should be testing
isempty(app.ValeurdeXEditField.Value) || isempty(app.fxEditField.Value) || isempty(app.fdeX.Value)
or perhaps isempty(strtrim(...))
另请参阅
类别
在 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!