Printing values to the command window

472 visualizaciones (últimos 30 días)
Isabella
Isabella el 12 de En. de 2012
Comentada: Image Analyst el 21 de En. de 2017
how does one print a value of a variable to the command window?
  2 comentarios
Matthew White
Matthew White el 21 de En. de 2017
type the variable in the script window without a semicolon then run the code
Image Analyst
Image Analyst el 21 de En. de 2017
You got it backwards. Typing the variable name first and then running the code will likely get you a "variable name not defined" error message. You have to do it like I said in my answer below. Run the code first, then execute the variable name without a semicolon.

Iniciar sesión para comentar.

Respuestas (2)

Image Analyst
Image Analyst el 12 de En. de 2012
Or you can simply list the variable all by itself on a line of code:
m = 10.42;
fprintf('m = %f\n', m); % Method 1
disp(m); % Method 2
m % Method 3. Note: no semicolon, just the name.
str = sprintf('%f\n', m) % Yet another method - returns a string

Honglei Chen
Honglei Chen el 12 de En. de 2012
Use fprintf
doc fprintf
  1 comentario
Walter Roberson
Walter Roberson el 12 de En. de 2012
or disp()
Isabella, a couple of technical notes:
- if you do not specify any file identifier for the fprintf() call, then the default is to go to the command window
- the file identifier 1 (the number) is defined as going to the command window.

Iniciar sesión para comentar.

Categorías

Más información sobre Shifting and Sorting Matrices 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