Using the C++ Math Library | ![]() ![]() |
Input and Output
MATLAB supports several functions for input and output. The simplest one is disp()
, short for display. Pass disp()
an array, and the array appears on the terminal screen. For example:
disp('Hello World');
Here, 'Hello World'
is a string array.
One group of I/O functions in MATLAB are like their namesakes in the C programming language. MATLAB supports fprintf()
, sprintf()
, scanf(),
and sscanf()
. fprintf()
prints to files, sprintf()
to strings, while scanf()
and sscanf()
read from files and strings, respectively. The arguments to these functions can be simple or complex. For example:
sprintf('The answer is: %f\n', magic(2));
This call creates this string array:
The answer is: 1.000000 The answer is: 4.000000 The answer is: 3.000000 The answer is: 2.000000
Notice how the sprintf()
command recycled its format string argument through the four elements of its data argument.
The I/O functions load()
and save()
allow you to save array variables from your application to what's called a MAT-file. That data can then be loaded back in by your application or by another application.
See Example - Using load() and save() (ex7.cpp) and Example - Using File I/O Functions (ex6.cpp) in Chapter 8 for more details on MATLAB input and output.
![]() | Functions | Errors | ![]() |