crear variable para contener un número en matlab
Frecuentes
Visto 169 veces
2 Respuestas
2
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
1
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 matlab variables or haz tu propia pregunta.
If your variable is named
X
. Solo agregaX=0;
at the beginning of your function (after the linefunction...
). - Oli