Borrar filtros
Borrar filtros

Variable definition in a simple function

2 visualizaciones (últimos 30 días)
Haojie
Haojie el 17 de Jul. de 2012
Hi all, I have a very simple function and cant get it work. I try to let t0(1)=t(2), t0(2)=t(3)... and write this function below. However it will show an error when I type t0=resize(t); in the command window. I have already defined t in another function.
Error in ==> resize at 2
k=2;
??? Output argument "t0" (and maybe others) not assigned during call to
"D:\HJ\Documents\MATLAB\resize.m>resize".
function t0=resize(t)
k=1;
while (k<size(t));
t0(k)=t(k+1);
k=k+1;
end
Your help is much appreciated.

Respuesta aceptada

Walter Roberson
Walter Roberson el 17 de Jul. de 2012
size(t) is always going to return a vector, so your expression k<size(t) is going to be comparing k to a vector of values.
An evaluated expression is true when the result is nonempty and contains all nonzero elements (logical or real numeric). Otherwise, the expression is false.
Therefor the "if" will be false if there is any dimension within t for which 1 < size(t) is false. Which would occur if t is a vector (row vector or column vector) as the other dimension of a vector is 1, and 1 < 1 is false.
If you examine your code, if the very first iteration of the "while" does not occur, then you do not create t0 at all.
I suggest you read about length()
  1 comentario
Haojie
Haojie el 17 de Jul. de 2012
Thank you. I am a newbie on matlab, so I didnt check carefully of the size function. Seems I need to do more study on the manual first

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by