Προς το περιεχόμενο

εντολή στο octave


maniac89

Προτεινόμενες αναρτήσεις

Δημοσ.

μπορεί να μου πει πως θα φτιάξω ένα script- M file στο

Οctave...

π.χ. edit hello.m

το αποθηκεύω μετά πατάω στο command line hello

αλλά δεν τρέχει......

όποιος γνωρίζει ας μου απαντήσει...

προκαταβολικά thanks...

Δημοσ.

http://wiki.aims.ac.za/mediawiki/index.php/Octave:Getting_started

Script files

 

It is useful to be able to save Octave commands and rerun them later on. You might want to save your work or create code that can be reused (by yourself or somebody else). Such files are known as Octave script files. They should be saved with a .m extension so that Octave can recognise them. (The .m extension is used because MATLAB calls its script files M-files and Octave is based on MATLAB.)

 

To run an existing script in Octave, you have to be in the same directory as the script file and type in the name of the file without the .m in Octave. For example, if I have a script called myscript.m in an octave directory, the following two commands will execute the script.

 

chdir('~/octave'); % This changes to the octave directory

myscript;

 

Note that the chdir('~/octave') command is necessary only if you are not already inside that directory when running Octave.

 

http://www.network-theory.co.uk/docs/octave3/index.html

 

http://linuxgazette.net/112/odonovan.html

Octave Scripts

 

Octave script files are simply a sequence of Octave commands which are evaluated as if you had typed them at the Octave prompt yourself. They are useful for setting up simulations where you wish to vary certain parameters for each run without having to re-type the commands each time, for sequences of commands that do not logically belong in a function and for automating certain tasks.

 

Octave scripts are placed in a file with the .m extension in the same way that functions are but a script file must not begin with the function keyword. Note that variables defined in a script share the same namespace (or scope) as variables defined at the Octave prompt.

 

The following is a simple example of an Octave script which calculates the time taken to create an array of integers of size specified by the user:

 

# An example Octave script

 

len = input( "What size array do you wish to use for the evaluation: " );

 

clear a;

tic();

for i=1:len

a(i) = i;

endfor

time1 = toc();

 

a = [1];

tic();

for i=2:len

a = [a i];

endfor

time2 = toc();

 

a=zeros( len, 1 );

tic();

for i=1:len

a(i) = i;

endfor

time3 = toc();

 

printf( "The time taken for method 1 was %.4f seconds\n", time1 );

printf( "The time taken for method 2 was %.4f seconds\n", time2 );

printf( "The time taken for method 3 was %.4f seconds\n", time3 );

 

Listing 6: lg_calc_time.m

 

And once it is saved in an appropriately named file, it can be executed as follows:

 

octave:13> lg_calc_time

What size array do you wish to use for the evaluation: 20000

The time taken for method 1 was 1.3509 seconds

The time taken for method 2 was 12.8984 seconds

The time taken for method 3 was 0.2850 seconds

octave:14>

 

Executable Octave Scripts

 

It is also possible to have executable Octave script files like we have executable Bash scripts. This is an excellent feature of Octave that can be very useful for large problems where a mixture of programs, tools or applications may be needed to solve the problem.

 

The example in listing 6 above can be easily converted to an executable Octave program by adding a single line to the beginning:

 

#! /usr/bin/octave -qf

 

# An example executable Octave script

 

len = input( "What size array do you wish to use for the evaluation: " );

... ... ... ... ... ... ... ... ...

printf( "The time taken for method 3 was %.4f seconds\n", time3 );

 

Listing 7: lg_calc_time.sh

 

Just make sure that the path to the Octave program is correct for your system (/usr/bin/octave), make the script executable and run it as follows:

 

[barry@hiscomputer lg]$ chmod u+x lg_calc_time.sh

[barry@hiscomputer lg]$ ./lg_calc_time.sh

What size array do you wish to use for the evaluation: 20000

The time taken for method 1 was 1.3959 seconds

The time taken for method 2 was 13.0201 seconds

The time taken for method 3 was 0.2800 seconds

[barry@hiscomputer lg]$

 

If you call the script with command line arguments, then they will be available through the built-in variable argv and the number of arguments will be contained in the variable nargin which we have already seen. A simple example of this can be seen in lg_calc_time2.sh which is the same as the last example except it reads the size of the array from the command line.

.

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...