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: Eric
Subject: Re: Help parsing a text file and assigning variables
Date: Sat, 04 Jul 2015 17:24:59 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0

On 07/04/2015 03:20 PM, Juan Pablo Carbajal wrote:
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.


Thank you,

I should not of included the '---------------------" as it is not part of the text file and I just put them in to separate the text file from the rest of the email.

I did get a solution based on your recommendation and some more searching I found on the matlab central website:


f = fopen("ex.cir");
inputs = textscan(f, '%s %d %d %f','delimiter', ' ');
fclose(f);

variables = genvarname(inputs{1,1});
values = inputs{1,4};

for i = 1:size(variables,1)
        eval([cell2mat(variables(i)) '= values(i);']);
end

Thank you,

Eric Meddleton



reply via email to

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