Message Boards Message Boards

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

GROUPS:

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
Answer
13 days ago

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
Answer
12 days ago

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
Answer
12 days ago

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
Answer
11 days ago

Hi delfino,

Thanks for your support!! I have attached my input text file posit_1.txt below.

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
Answer
10 days ago

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
Answer
1 hour ago

Group Abstract Group Abstract