Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Scilab - reading numbers from a text file

34 views
Skip to first unread message

littleshopgang

unread,
Jul 13, 2010, 6:35:28 AM7/13/10
to
I have a text file that has both string and numbers in it. It looks
something like this:

density
12345
viscosity
678910
surface tension
11121314
........

and so on. the file is called "properties.txt" and it is a list of
fluid properties that I need to use for my calculations.

What command could I use to extract the numbers from the text file and
assign them to variables in scilab. I have tried:

mopen
file('open',...)
fscanfMat
read

nothing seems to get the job done. fscanfMat came close but it reads
only the first number and not the rest of them.

I would appreciate it if someone could suggest a method of extracting
the numbers as, say a column vector to be later assigned to different
variables or directly assign them from the text file that has the
data.

Thank you
Vikram

Rich Webb

unread,
Jul 13, 2010, 8:40:52 AM7/13/10
to

In a case like this, I would pass the source file through another "pre-
processor" application such as Perl. I'm sure that Scilab is general-
purpose enough to do the file manipulation with native functions but a
tool like Perl (available for free for pretty much every O/S out there)
seems to be a better fit for text file operations.

--
Rich Webb Norfolk, VA

fake...@invalid.domain

unread,
Jul 13, 2010, 5:15:09 PM7/13/10
to
In article <7624d6f7-7065-4c10...@s24g2000pri.googlegroups.com>,

On a mac/unix or with cgywin
sed -e 's/ //' -e 's/\([0-9]\)$/\1];/' -e 's/\([^;]\)$/\1=[/' text > text2

so the file now looks like
density=[
12345];
viscosity=[
678910];
surfacetension=[
11121314];

and one can
exec text2
-->density=[
-->12345];

-->viscosity=[
-->678910];

-->surfacetension=[
-->11121314];


--
Steven Bellenot http://www.math.fsu.edu/~bellenot
Professor and Associate Chair phone: (850) 644-7405
Department of Mathematics office: 223 Love
Florida State University email: bellenot at math.fsu.edu

Message has been deleted

bruno

unread,
Jul 14, 2010, 4:30:27 AM7/14/10
to
On 13 juil, 12:35, littleshopgang <trivikra...@gmail.com> wrote:
> I have a text file that has both string and numbers in it. It looks
> something like this:
>
> density
> 12345
> viscosity
> 678910
> surface tension
> 11121314
> ........
>
> and so on. the file is called "properties.txt" and it is a list of
> fluid properties that I need to use for my calculations.

A possibility is the following :

[fd,err]=mopen("properties.txt");
txt=mgetl(fd); // get all lines
mclose(fd);
prop = txt(1:2:$); // extract properties names
val = evstr(txt(2:2:$)); // extract values

hth
Bruno

Rich Webb

unread,
Jul 14, 2010, 8:13:21 AM7/14/10
to
On 13 Jul 2010 21:15:09 GMT, fake...@invalid.domain wrote:

>On a mac/unix or with cgywin
>sed -e 's/ //' -e 's/\([0-9]\)$/\1];/' -e 's/\([^;]\)$/\1=[/' text > text2
>
>so the file now looks like
>density=[
>12345];
>viscosity=[
>678910];
>surfacetension=[
>11121314];

Very nice!

If the OP is on an MS Windows box, he may also want to try MinGW + MSYS
for a lighter-weight but still decent shell, utility, and compiler
environment.

0 new messages