help-octave
[Top][All Lists]
Advanced

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

Re: Help parsing a text file and assigning variables


From: Juan Pablo Carbajal
Subject: Re: Help parsing a text file and assigning variables
Date: Sun, 5 Jul 2015 00:20:09 +0200

On Sat, Jul 4, 2015 at 11:06 PM, Eric <address@hidden> wrote:
> Hello,
>
> I am trying to read a simple spice netlist file into octave and assign the
> variables listed with the values listed in the file.  The file is a text
> file.
>
> An example of the file I am trying to import is:
>
> ------------------------------------------------------
> V1 1 0 12
> R1 1 2 1000
> R2 2 0 2000
> R3 2 0 2000
>
> -------------------------------------------------------
>
> I would like to read the file and automatically create and assign the
> variables:
>
> V1 = 12
> R1 = 1000
> R2 = 2000
> R3 = 2000
>
> where the first element of each line is the variable name and the fourth
> element of each line is the value.
>
> I have been looking through google and the octave documents for some
> examples but have not been successful.
>
>
> Thank you for any help.
>
>
> Eric Meddleton
>
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/help-octave

Have you tried something like

f = fopen ("data.txt");
nvar = 4;
for i = 1:nvar
   [var x y z] = fscanf (f,"%s %d %d %d","C")
   eval (sprintf ("%s = %d", var, z))
endfor

Where data.txt is your file (you have remove the "-----------------")
and nvar is the number of variables you want to read.
You can improve from here, I guess.



reply via email to

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