Message Boards Message Boards

0
|
5746 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Importing data from a (structured) file

Posted 11 years ago
Hi,

I have data in the following format. I have aroung 3000 entries and it's tiresome to enter it manually. Is there any way that I can import these entries from a file.

k_e[1][1] =a
k_e[1][2] =0
k_e[1][3] =0
k_e[1][4] =-1754472.1689059502
k_e[1][5] =0[1][25] =0
k_e[1][26] =0
k_e[1][27] =0
k_e[2][1] =0
k_e[2][2] =b
k_e[2][3] =86831.960212348175
k_e[2][4] =0
k_e[2][5] =-31999.493974608162
k_e[2][6] =86831.960212348175
k_e[2][7] =0
POSTED BY: Usman Rauf
4 Replies
Posted 11 years ago
Hi Usman,

I am not very familiar with matlab syntax -- but it appears you want to execute these statements to produce an array in Mathematica, as they would in Matlab.  This is possible because of course both systems define array and the means of setting element values. However, the syntax differs. We need to translate the Matlab syntax to Mathematica.

Mathematica defines arrays and higher order tensors as lists of lists. Matlab appears to use a single square braket set to designate an element part, but Mathematica uses a double bracket. In Mathematica, the third row second element of the matrix m is given as m[[3,2]] or m[[3]][[2]]. So we need this translation. Also, the underscore is a reserved character used in pattern definitions, so it must be eliminated.

The below code does both of these things, which translates the statements. Then it defined a matrix with default values, here 0, but they could be anything including Null. It then executes the statements by interpreting the translated strings as input.

I give the code below. The output has been deleted so you get something you can cut and paste. If you paste this into a notebook and execute it, it will reproduce the result. By dividing the lines and removing semicolons you can see each output as it executes. There may well be a more elegant way to do this, but this works. At least as long as the input so constructed from the rest of your data produces valid code.
 (* set the file name *)
 fileName = "C:\\Users\\David\\Documents\\Mathematica \
 Files\\import\\data2.txt";
 
 (* read the file as a list of strings *)
 raw = ReadList[fileName, String];
 
 (* change the syntax to mathematica *)
 executable = StringReplace[raw, {"_" -> "u", "[" -> "[[", "]" -> "]]"}];

(* define kue as a 4x4 array with default values *)
kue = Table[0, {4}, {4}];

(* execute the translated expressions *)
ToExpression /@ executable;

kue // TableForm
POSTED BY: David Keith
Posted 11 years ago
This seems a little confusing. What if I want to import following as an input ?
k_e[1][1] =a
k_e[1][2] =0
k_e[1][3] =0
k_e[1][4] =-1754472.1689059502

Actually k_e is a list or array. and I am generating these values from Matlab and these will be use as an input specifically speaking as an entries of a 30*30 matrix. what If I just want to read it as an element of a matrix ? K is a matrix which have 4*4 order. each element in the text file which is to be imported is also have same name for the variables. one way is to simply copy and paste and put a semi colon at the end of every entry. thats a laboriaous. now point is how to import those matrix entries as it is if they all are comma seperated format. 

Have I explained properly ?
POSTED BY: Usman Rauf
Posted 11 years ago
On the other hand, if what you want is that Mathematica interpret this text as input, that can also be done, with reservations. The code below reads in the text as a list of strings. It replaces all the underscores with "u", since underscore is reserved. It then applies ToExpression to each. (Note that there is a syntax error in the input where a numeric character appears as a symbol.) The last line verifies an assignment was made.

 fileName = "C:\\Users\\David\\Documents\\Mathematica \
 Files\\import\\data.txt";
 
 raw = ReadList[fileName, String];
 
 replaceUnderscore[str_] := StringReplace[str, "_" -> "u"];
 
 executable = replaceUnderscore /@ raw;
 
ToExpression /@ executable;

kue[1][1]
POSTED BY: David Keith
Posted 11 years ago
Hi Usman,
It ia a bit difficult to tell what you want from the import. But assuming you are interested in the numeric values, here is a code that extracts them. It produces a list of the values after the "=" in each line. As it also extracts the characters before the "=" as a string, so with slight modification that could be done as well.
Best,
David
 fileName = "C:\\Users\\David\\Documents\\Mathematica \
 Files\\import\\data.txt";
 
 (* read the file as records of words separated by "="  *)
 raw = ReadList[fileName, Word, RecordLists -> True,
   WordSeparators -> "="];
 
 (* function to make a number if possible, else a string *)
 makeNumber[str_] :=
If[NumberQ[ToExpression[str]], ToExpression[str], str]

(* take the last word in each record and make it a string or a number *)
lastWords = makeNumber /@ Last /@ raw
POSTED BY: David Keith
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