如何在 Octave GNU 中输出
Octave 是开源的,可在许多平台上免费使用。它是一种高级语言。它带有一个文本界面和一个实验性的图形界面。它还用于解决各种数值问题的各种机器学习算法。可以说它类似于 MATLAB 但比 MATLAB 慢。
有多个库函数可以在 Octave 中显示输出。
打印函数()
printf()函数打印字符并返回打印的字符数,格式为以 % 开头并以转换字符结尾的字符串(如 c、i、f、d 等)。
Syntax : printf(template, variables)
Parameters :
- template : string to be written on the console, it also contains format specifiers like %d, %s etc
- variables : values that will replace the format specifiers
Types of Format Specifiers :
- %d : used for integers
- %f : used for floating point values
- %s : used for strings
Returns : number of characters printed
例子 :
MATLAB
% using printf() without format specifiers
printf("Hello World!!\n");
x = 5;
% using printf() with format specifiers
printf("Welcome, value of x is %d\n", x);
MATLAB
% using fprintf() without format specifiers
fprintf("Hello World!!\n");
x = 5;
% using fprintf() with format specifiers
printf("Welcome, value of x is %d\n", x);
MATLAB
% using sprintf() without format specifiers
fprintf("Hello World!!\n");
x = 5;
% using sprintf() with format specifiers
printf("Welcome, value of x is %d\n", x);
MATLAB
% displaying the value of the console
disp("Hello World!!");
% assigning the value to a variable
output = disp("Welcome");
% displaying the assigned variable value
disp(output);
输出 :
Hello World!!
Welcome, value of x is 5
fprintf()
fprintf()函数等效于 printf()函数,只是输出被写入文件描述符 fid 而不是 stdout。
Syntax : fprintf(fid, template, variables)
Parameters :
- fid : file descriptor
- template : string to be written on the console, it also contains format specifiers like %d, %s etc
- variables : values that will replace the format specifiers
Types of Format Specifiers :
- %d : used for integers
- %f : used for floating point values
- %s : used for strings
Returns : number of characters printed
例子 :
MATLAB
% using fprintf() without format specifiers
fprintf("Hello World!!\n");
x = 5;
% using fprintf() with format specifiers
printf("Welcome, value of x is %d\n", x);
输出 :
Hello World!!
Welcome, value of x is 5
sprintf()
sprintf()函数类似于 printf()函数,不同之处在于输出以字符串形式返回。
Syntax : sprintf(template, variables)
Parameters :
- template : string to be written on the console, it also contains format specifiers like %d, %s etc
- variables : values that will replace the format specifiers
Types of Format Specifiers :
- %d : used for integers
- %f : used for floating point values
- %s : used for strings
Returns : number of characters printed
MATLAB
% using sprintf() without format specifiers
fprintf("Hello World!!\n");
x = 5;
% using sprintf() with format specifiers
printf("Welcome, value of x is %d\n", x);
输出 :
Hello World!!
Welcome, value of x is 5
显示()
disp()函数用于在控制台上显示值或将值分配给变量。
Syntax : disp(argument)
Parameters :
- argument : can either be a string or a variable
Returns : the string/value printed
例子:
MATLAB
% displaying the value of the console
disp("Hello World!!");
% assigning the value to a variable
output = disp("Welcome");
% displaying the assigned variable value
disp(output);
输出 :
Hello World!!
Welcome