Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB console output

Say I had a variable called "x" and x=5.

I would like to do:

disp('x is equal to ' + x +'.'); 

and have that code print:

x is equal to 5.

This is how I am used to doing things in Java, so their must be a similar way to do this in MATLAB.

Thanks

like image 820
JJJ Avatar asked Oct 05 '11 04:10

JJJ


People also ask

How do I display output in MATLAB?

disp( X ) displays the value of variable X without printing the variable name. Another way to display a variable is to type its name, which displays a leading “ X = ” before the value. If a variable contains an empty array, disp returns without displaying anything.

How do I show the console in MATLAB?

The Command Window is always open. To restore the Command Window to the default location, go to the Home tab, and in the Environment section, click Layout. Then, select from one of the default layout options. To bring focus to the Command Window from another tool such as the Editor, type commandwindow .

How do I print a Command Window in MATLAB?

The contents of MATLAB's Command Window can be printed out by using 'Ctrl + P' or right-clicking and selecting "Print..." from the Command Window itself.

What is the console in MATLAB?

The "MATLAB Console" tool allows Origin users to issue MATLAB commands from within the Origin environment, and transfer data between the two applications either using a graphical interface, or by issuing commands. The Console requires both Origin and MATLAB be installed on the same computer.


2 Answers

If you want to use disp, you can construct the string to display like so:

disp(['x is equal to ',num2str(x),'.']) 

I personally prefer to use fprintf, which would use the following syntax (and gives me some control over formatting of the value of x)

fprintf('x is equal to %6.2f.\n',x); 

You can, of course, also supply x as string, and get the same output as disp (give or take a few line breaks).

fprintf('x is equal to %s\n',num2str(x)) 
like image 54
Jonas Avatar answered Sep 20 '22 20:09

Jonas


printing out a few scalar variables in matlab is a mess (see answer above). having a function like this in your search path helps:

function echo(varargin) str = ''; for k=1:length(varargin)     str = [str ' ' num2str(varargin{k})]; end  disp(str) 
like image 36
johannes_lalala Avatar answered Sep 20 '22 20:09

johannes_lalala