help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: opening txt-file,script under octav


From: Israel Herraiz
Subject: Re: opening txt-file,script under octav
Date: Wed, 02 May 2012 12:48:38 -0700
User-agent: Sup/git

Excerpts from kathy_plotnikova's message of Wed May 02 12:18:49 -0700 2012:
> #!/bin/bash
> #include <stdio.h>

Those two lines are not Octave code. If you want to make a script with
Octave. Have a look at the manual if you want to build scripts with
Octave:

http://www.gnu.org/software/octave/doc/interpreter/Executable-Octave-Programs.html#Executable-Octave-Programs

But note that you don't need to make a shell script to run your code
with Octave. Just put your code in a file ending in ".m", run Octave,
go the directory where your file is, and write the name of the file
(without the ".m").

> fid = fopen('k1.csv','rt');
> Z = textscan(fid,9);

The second argument is the format of the strings you want to read. But
if you are reading CSV files, have a look at the csvread or dlmread
functions.

> fclose(fid);
> echo "Z is $Z";

If you want to display the value of Z, use this instead (assuming Z is integer):

fprintf('Z is %d\n', Z); 

echo has a different meaning in Octave.

> And also, is it possible to work wirk with txt data files instead of csv
> data files?

If you use textscan, you can read any text format. For instance, say
that you have integers separated by tabs, and you have three numbers
in each line, you can use

Z = fscanf(fid,'%d\t%d\t%d\n',[3 Inf]);

You will get a matrix where each column is the corresponding line in
the text file. If you want the same order as in the text file, write
Z=Z';

But if you want to read all the numbers in a vector just write
Z = fscanf(fid,'%d').

Hope this helps.

Israel

-- 
Israel Herraiz
Assistant Professor / Profesor Ayudante Doctor
Technical University of Madrid (UPM)
http://mat.caminos.upm.es/~iht/
Tel: +34 91 336 6663


reply via email to

[Prev in Thread] Current Thread [Next in Thread]