Matlab function doesnt run when called from AppDesigner
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Sara Rodriguez
il 14 Nov 2021
Commentato: Sara Rodriguez
il 16 Nov 2021
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/799934/image.jpeg)
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;"
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/799939/image.jpeg)
5 Commenti
Dave B
il 15 Nov 2021
Modificato: Dave B
il 15 Nov 2021
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);
Risposta accettata
Dave B
il 15 Nov 2021
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 Commenti
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!