Octave - Plot, Control Statements

Octave

Plotting Data:

plot(x,y) - plot
plot(x,y,'r') - plot with color red
hold on - keep current plot on the screen, so we can add more plots at the same time
xlabel('value') - add a label to x-axis
legend('x legend', 'y legend') - add legend for each plot
title('my plot)
print -dpng 'myPlot.png' - generate png file of the plot
close - close plot window
figure(1); plot(x,y) - allow multiple plot windows
clf - clear a figure
subplot(1,2,1) - divides plot into a 1x2 grid, then access 1st (left) element
axis([0.5 1 -1 1]) - change x range and y range
imagesc(A) - create a grid representation of matrix A with different color for each cell
Below is result of: "imagesc(A), colorbar, colormap gray" where A = magic(5)

Control Statements:

for i=1:10,
  v(i) = 2^i;
end;
i=1;
while i <= 5,
  v(i) = 100;
  i = i + 1;
end;
if (v == 1),
  disp('The value is one');
elseif (v == 2),
  disp('The value is two');
else
  disp('The value is not one or two');
end;

Function:

Octave looks for file name with extension .m for functions in current directory. To include other directory/path, use addpath
File name must be the same as function name. In this case squareThisNumber.m
function y = squareThisNumber(x)
y = x^2
To return multiple values:
function [y1, y2] = squareAndCubeThisNumber(x)
y1 = x^2
y2 = x^3
Example:
X = [1 1; 1 2; 1 3], y = [1; 2; 3], a = [0; 1]


Comments