User Tools

Site Tools


octave:octave

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
octave:octave [2008/06/25 10:05] devaoctave:octave [2009/04/23 13:12] deva
Line 1: Line 1:
 =====Octave syntax and library overview===== =====Octave syntax and library overview=====
 +====Installing packages====
 +Fetch the package files from [[http://octave.sourceforge.net/|octaveforge]].
 +Then create (or add to) the file ~/.octaverc the following content:
 +<code matlab>
 +addpath(genpath('~/octave'))
 +</code>
 +And run ''pkg install packagename-1.0.0.tar.gz'' or whatever the package is called from inside octave.
 +Now the package should be available.
 +
 +====plotting====
 +<code matlab>
 +h = [1,2,3,4,5];
 +plot(h);
 +xlabel("some numbers");
 +print("myfile.pdf", "-dpdf");
 +</code>
 +This code plots a a vector with a label on the x-axis, and exports it to a pdf file.
 +
 +====function====
 +<code matlab>
 +function retval = myfunction(param1, param2)
 +  retval = param1 + param2;
 +end;
 +</code>
 +
 +====strings====
 +<code matlab>
 +mystring = 'hello world'; % create string
 +sizeof(mystring) % get length of string
 +
 +mystrilyng2 = [ mystring, 'fisk']; % concatenate two strings
 +
 +</code>
 +sizeof on works in octave. in matlab the length of a string cannot be obtained!
 +
 +====string array====
 +<code matlab>
 +myemptyarray = {}; % create empty string array.
 +
 +myarray = { 'hello', 'world' }; % create array with two strings.
 +myarray(1) % prints 'hello'
 +myarray(3) = 'blah'; %inserts another string into the array.
 +
 +sizeof(myarray) % get number of elements
 +sizeof(myarray(1)) %get length of first string
 +
 +% Iterate strings in string array
 +for str = myarray
 +  disp(str)
 +end
 +
 +</code>
 +Note: Arrays are 1-based, not 0-based as they usually are in real programming languages.
 +Note: sizeof is an octave-only function. size can be used to get the array size in matlab.
 +
 ====if then else==== ====if then else====
 <code matlab> <code matlab>
Line 29: Line 84:
 end;  end; 
 </code> </code>
 +
 +====switch case====
 +<code matlab>
 +yesno = "yes"
 +
 +switch yesno
 +case {"Yes" "yes" "YES" "y" "Y"}
 +  value = 1;
 +case {"No" "no" "NO" "n" "N"}
 +  value = 0;
 +case "blah"
 +  value = 42;
 +otherwise
 +  error ("invalid value");
 +endswitch
 +</code>
 +Note: In matlab the ''endswitch'' should be replaced by ''end''
  
 ====Links==== ====Links====
octave/octave.txt · Last modified: 2009/04/23 13:54 by deva