Octave
General
-
;{.verbatim} semicolon at the end of a command supresses the
output of the command
-
typeinfo{.verbatim} - check data type -
close all{.verbatim} -
clear{.verbatim} -
clc{.verbatim}
Plot
view(azimuth, elevation)
view(90,0) - To view 3d plot as 2d
view(-90,0)
% Octave Legend: <https://octave.sourceforge.io/octave/function/legend.html>
File handling operations
data=load('file_name.mat') - To load a mat file
save("-text", "data.txt", "data") - To save a variable to a file
save data
csvwrite (filename, x) - Write the numeric matrix x to the file filename in comma-separated-value (CSV) format.
data=[struct2cell(load('data.mat')){:}]; - convert a structure to array
c=exist(name); % [check for file/var exists](https://octave.sourceforge.io/octave/function/exist.html)
Reading a binary file
fid=fopen("file.bin", "rb");
data=fread(fid, "uint64");
Loading file into a variable
data=load("file.mat")
Save to file in plain text
dlmwrite("file.txt", data, ",")
csvwrite("file.csv", data)
Structures
data_main.name % will get data from the data_main structure, with object "name"
String manipulation
strrep("String", "i", "1"); % i will be replaced with 1; Str1ng
strcat("String1", "String2"); % Output = String1String2
str=int2str(i) % Integer to string
num=str2num(j) % String to number
Matrix manipulation
Find sizes
ndims(matrix)
rows(matrix)
columns(matrix)
Rearranging - flip
flipr(matrix) % 12...9 to 98...1
Rearranging - permute
%% Converting x, y and z to z, x, y
data.element=permute(data.element, [3 1 2]);
size(data.rdc1);
Sum all elements of a matirx
sum(matrix)
Multiply all elements in a matrix with itself
matrix.*matrix
Concatenation
Matrix
- hcat
``` octave
horzcat (array1, array2, …, arrayN)
```
Return the horizontal concatenation of N-D array objects, array1,
array2, …, arrayN along dimension 2.
Arrays may also be concatenated horizontally using the syntax for
creating new matrices. For example:
``` octave
hcat = [ array1, array2, … ]
```
- vcat
``` octave
vertcat (array1, array2, …, arrayN)
```
Return the vertical concatenation of N-D array objects, array1,
array2, …, arrayN along dimension 1.
Arrays may also be concatenated vertically using the syntax for
creating new matrices. For example:
``` octave
vcat = [ array1; array2; … ]
```
Conditional statements
https://octave.org/doc/v5.2.0/The-if-Statement.html
if (condition)
%% then-body
elseif (condition)
%% elseif-body
else
%% else-body
endif
Functions
https://octave.org/doc/v4.4.1/Defining-Functions.html
function name (arg-list)
%% body
endfunction
Plot
Example
t = 0:0.01:2*pi;
x = sin (t);
plot (t, x);
title ("one cycle of a sine wave");
Colors
colormap jet{.verbatim}
Figure
- Multiple plots/ figures
figure{.verbatim} This will create a new figure and any plot
command below it will plot in the new figure. Old figures will not
be changed.
- Figure position
https://stackoverflow.com/questions/5183047/setting-graph-figure-size`
``` octave
figure(‘position’, [0, 0, 2000, 1000]) % create new figure with specified size
```
Labels
xlabel "x name";
ylabel "y name";
title ("title name");
Subplots
subplot(2,2,1); % 2 rows, 2 columns, i.e. 2x2; 1st plot
plot(data);
subplot(2,2,2);
mesh(data2);
subplot(2,2,3);
mesh(data3);
subplot(2,2,4);
mesh(data4);
3D plots
plot(data(1,:,1)) % 2D
mesh(data(1,:,:)) % 3D
Saving plots
print -dpng filename
%% or
print("filename.jpg")
Loops
- For loop
``` octave
fib = ones (1, 10);
for i = 3:10
fib(i) = fib(i-1) + fib(i-2);
endfor
for i=1:96
name=strcat(“text_”, sprintf("%d", i), “_text2.png”);
endfor
```
Errors
#error
<2021-09-01 Wed> "GL2PS warning: unknown token in buffer" [[octave]{.smallcaps}]{.tag tag-name=“octave”} [[blog]{.smallcaps}]{.tag tag-name=“blog”} [[error]{.smallcaps}]{.tag tag-name=“error”}
Fix
close all; graphics_toolkit ("gnuplot");