Matlab function doesnt run when called from AppDesigner

17 次查看(过去 30 天)
I have a problem in which if I call a function from Command Window it functions properly, but when I call it from App Designer it says there is a mistake in the matrix multiplication dimensions. I think this is very weird, because if there was a mistake related to a matrix shouldn't it show in both modes??? I've been using it from command line for a while and its been working fine, Im not sure why it doesnt run from AppDesigner.
This is the code :
function [resultCode, resultX1, resultX2, Z] = busquedas_function(Xo, Delta, Iter, Fun)
resultCode = 0;
% 1 = success
% 2 = failed
%input es un comando de solicitud de entrada de datos del usuario.
f=inline(Fun);
Yo=f(Xo);
if Yo==0
fprintf('\n\nSOLUCION:\n');
fprintf ('Xo es raiz\n\n');
else
X1=Xo+Delta;
Cont=1;
Y1=f(X1);
Z=[Cont,Xo,Yo,(Yo.*Y1)];
%Z es una matriz la cual permitira observar lo datos como una tabla a la
%finalizacion del programa
%La sentencia While ejecuta todas las órdenes mientras la expresión sea
%verdadera.
while (((Yo*Y1)>0) & (Cont<Iter ))
Xo=X1;
Yo=Y1;
X1=Xo+Delta;
Y1=f(X1);
Cont=Cont+1;
Z(Cont,1)=Cont;
Z(Cont,2)=Xo;
Z(Cont,3)=Yo;
Z(Cont,4)=Yo*Y1;
%las z son las posiciones asignadas en la tabla a los resultados que se
% observarán
end
if Y1==0
fprintf('\n\nSOLUCION:\n')
fprintf('%g es raiz\n\n',X1)
resultX1 = X1;
resultX2 = X1;
else
if Yo*Y1<0
fprintf('\n\nSOLUCION:\n')
fprintf ('El intervalo que contiene la raíz es[%g,%g]\n\n',Xo,X1)
resultCode = 1;
resultX1 = Xo;
resultX2 = X1;
else
fprintf('\n\nSOLUCION:\n')
fprintf ('Fracaso en %g iteraciones\n\n',Iter)
resultCode = 2;
resultX1 = 0;
resultX2 = 0;
end
end
end
fprintf('TABLA\n\nIteraciones Xo Yo Yo*Y1 \n\n');
disp(Z);
%La funcion disp permite visualizar la tabla, obtenida de los resultados de
%la secuencia while
ezplot(f);
%El comando ezplot permite grafica una función.
end
if I call it from command line it runs correctly
But calling it with same parameters from App Designer brings up this error:
"Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-11.
Error in busquedas_function (line 40)
Z(Cont,3)=Yo;"
  5 个评论
Sara Rodriguez
Sara Rodriguez 2021-11-15
I checked and by putting values on inside the App Designer code it does work (2nd commented line)
However I'm not sure why my Edit Fields are not passing the arguments as they should.
I double checked and Xo, Delta and Iter are numeric values as they should,Funcion is a text field, since I have funcion as a string I thought I needed to convert it to a char vector, so I tried converting it with:
Fun = app.FuncionEditField.Value
Func = convertStringsToChars(Fun) )
so Im a bit confused as to where is the bad casting.
I mean, it would seem like its the same data, at least to my clueless self.
Dave B
Dave B 2021-11-15
编辑:Dave B 2021-11-15
Are you sure your editfield values are numbers and not characters that contain numbers? If I do:
h=uieditfield;
And then type the number 0.5 in there, and then do h.Value, I get '0.5' not 5. I think you want str2num (I previously wrote num2str by accident sorry!)
Xo = str2num(app.XoEditField.Value);
Delta = str2num(app.DeltaEditField.Value);
Iter = str2num(app.IteraEditField.Value);

请先登录,再进行评论。

采纳的回答

Dave B
Dave B 2021-11-15
You're passing in characters containing numbers the values instead of the values themselves. Use str2num to convert before calling your function.
Xo = str2num(app.XoEditField.Value);
Delta = str2num(app.DeltaEditField.Value);
Iter = str2num(app.IteraEditField.Value);
For a robust app you might want to also validate that they are values before calling the function. You could do this with by checking the result of calling str2num, or by introducing an arguments block (with the mustBeNumeric validator) into busquedas_function.
  8 个评论
Dave B
Dave B 2021-11-16
编辑:Dave B 2021-11-16
Ah Sara I feel horrible that I led you down the wrong path to begin with, it's been forever since I've seen someone using inline, I should have spotted it right away! (but ' is so small!)
Sara Rodriguez
Sara Rodriguez 2021-11-16
Nooo, don't worry, I barely know what I'm doing and you've been very patient explaining what may be wrong with examples and everything! I'm very grateful!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by