Message Boards Message Boards

0
|
9346 Views
|
6 Replies
|
1 Total Likes
View groups...
Share
Share this post:

[?] Read a text file in the Wolfram language?

Hi , I am very new to Mathematica. I need to read inputs from the .text file for below code to perform arithematic operation i.e pfused=ad+bc

When I am trying with Import it is not performing operation, but its reading the text file, please any one help me to resolve the below issue.

v = Import["C:\\work\posit.txt", "Text"]
For[i = 1, i < Length[v], i = i + 4, a = v[i]  ; d = v[i + 3];  b = v[i + 1]; c = v[i + 2]]
PArith :=
Module[{pnuma = a, pnumd=d,pnumb=b,pnumc=c , pfused},
pfused= pnuma*pnumd+pnumb*pnumc ;
Print[{pnuma, pnumd, pnumb, pnumc, pfused}]];
Print[N[{pnuma, pnumd, pnumb, pnumc, pfused}, 37]];
Attachments:
POSTED BY: GABBAR JADAV
6 Replies

I added some numbers to your file. You presume they come in blocks of 4 so I created the file with 12 numbers.

v = Partition[ReadList["posit_1.txt"], 4]
(#[[1]] #[[4]] + #[[2]] #[[3]]) & /@ v

I Map a function where I switch the elements of each row I Map on to. The #[[1]] means the first item of the list so its the first * fourth + second * third.

Attachments:
POSTED BY: l van Veen

Gabbar,

You need to escape the backslashes and add the missing semicolons at the end of each command, or put your commands in multiple cells. In your code you are trying to extract elements from an undefined variable v.

After completing a tutorial like http://www.wolfram.com/language/fast-introduction-for-programmers/en/ everything will make sense.

Regards,

Gustavo

POSTED BY: Gustavo Delfino

Hello,

please any one help me in resolving the below issue

i have tried by changing by brackets in my code but its is not taking my input from text file to perform my arithmatic operation , please look in to my code and make necessary changes ,perform arithmatic operation for given text file inputs

Attachments:
POSTED BY: GABBAR JADAV

Grabbar,

You have a variety of issues in you question (missing brackets, incorrect calls to Take, referencing indices beyond the last value). Focus first on importing the data:

multilineString = Import["posit.txt", "Text"]

"64817367.784333

1234.566

2345.567

3456.890

123

234

456
"

Then split into lines:

listOfStrings = StringSplit[multilineString]

{"64817367.784333", "1234.566", "2345.567", "3456.890", "123", "234", "456"}

And convert the strings to numbers:

listOfNumbers = Map[ToExpression, listOfStrings]

{6.48174*10^7, 1234.57, 2345.57, 3456.89, 123, 234, 456}

After that you can use the Partition command to split the data in sets of 4 and then apply your formula.

POSTED BY: Gustavo Delfino

Hi Delfino,

I have attached my input text file posit.txt below, i have tried with [[ ]], as below

For[i = 0, i < Length[v], i = i ++, a = v [[i]]; d = Take v[[i + 3]]; b = Take  v[[i + 1]]; c = Take v[[i + 2]];

still I am unable to get the pfused = ad+bc

Please help me to resolve the same.

Attachments:
POSTED BY: GABBAR JADAV

Could you provide a sample "C:\work\posit.txt" ? You probably need to split your text into lines and pay attention to [ ] vs [[ ]]

POSTED BY: Gustavo Delfino
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract