crear variable para contener un número en matlab

In my function I'd create a variable that will contain simply one number. How do I initialize it?I have to initialize it to 0 value or I can initialize it simply putting in function var;

preguntado el 22 de septiembre de 12 a las 18:09

If your variable is named X. Solo agrega X=0; at the beginning of your function (after the line function...). -

2 Respuestas

In Matlab, variables are initialized dynamically. There is no C++ or Java equivalent of memory allocation versus initialization.

Por ejemplo:

Código C:

 int x; // Allocate memory on the stack for integer.
 x = 5; // Assign the value 5

Código de Matlab:

 x = 5;  %# Allocate memory and assign the value 5

Usted no puedes break this in two parts in the following way:

 x; %# SYNTAX ERROR! x is not defined yet.
 x = 5; 

Also, I am not sure whether you meant that var is some kind of a keyword, but in case you think it is - you're mistaken.

Respondido el 22 de Septiembre de 12 a las 23:09

var = 0;

Just set it to the number. No initialization needed. Matlab variables are fully dynamic.

También puedes hacer algo como

global myvar; 

and this will create a variable equal the the empty list []

You can overwrite variables too without worrying about types.

myvar %equal to []
myvar = 1543 % equal to 1543
myvar = 'a string hello' %Now myvar is a string
myvar = {item1, item2, item3} % now myvar is a cell array containing 3 items. 

Respondido el 22 de Septiembre de 12 a las 18:09

but the variable will take value during the execution and not at the beginning.so initially I have to put it to zero? - user1508419

Just a small point, if your first line of code is really the first and you have not defined myvar, it will throw an error. Other than that, a good answer - Andrey Rubshtein

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.