Write variables to a file

Forum for OpenSees users to post questions, comments, etc. on the use of the OpenSees interpreter, OpenSees.exe

Moderators: silvia, selimgunay, Moderators

Post Reply
lorcancon
Posts: 25
Joined: Thu May 22, 2014 5:22 am
Location: Roughan & O Donovan

Write variables to a file

Post by lorcancon »

All,

Is it possible to write variables to a file? for example, I have a numeric variable "X" which which changes through each loop of a simulation. I want to add the value of "X" to a file called "data" on each loop. I've tried the following command:

print -file data.out $X

but on each loop it just writes a new file which is named the value of "X" (i.e. a number).

Any help would be much appreciated.

Thanks,
Lorcan.
nskok
Posts: 17
Joined: Wed May 15, 2013 12:09 pm
Location: Purdue University

Re: Write variables to a file

Post by nskok »

You can use the "puts" command to write to a file. If you want to output a list onto separate lines, you'll have to add a loop.

set X 1000.
set fid [open data.out w]
puts $fid "$X"
close $fid

set X_List [list 1 2 3 4]
set fid [open data.out w]
foreach Xi X_List {
puts $fid "$Xi\n"
}
close $fid
lorcancon
Posts: 25
Joined: Thu May 22, 2014 5:22 am
Location: Roughan & O Donovan

Re: Write variables to a file

Post by lorcancon »

Nick,

Thanks a lot. Worked perfectly.

Lorcan.
Post Reply