Message Boards Message Boards

Import a matrix stored in a text file?

Posted 5 years ago

I am trying to import a very large matrix file to a variable using the code below. The matrix is of the form {{A,B,C},{D,E,D},{G,F,A}}

inputseq2s = Import[ "C:\Users\jdd0758\Desktop\WaveformsOnly.txt","Data"]

However when I import it like this, it is not read as if it was entered as:

inputseq2s = {{A,B,C},{D,E,D},{G,F,A}}

Extra commas are added to the input and it will not work in my algorithm. I have also tried importing it as "Mat" but that also did not work. How can I make this work properly?

POSTED BY: Jamie Dixson
4 Replies
Posted 5 years ago

Rohit, thank you for your help. Unfortunately this did not solve the problem that I was having. Ultimately inputseq2s = Import["C:/Users/jdd0758/Desktop/WaveformsOnly.txt", "String"]; ended up solving my problem.

POSTED BY: Jamie Dixson
Posted 5 years ago

InputForm reveals the issue

data = Import["~/Downloads/tESTmATRIX.txt", "List"];
InputForm@data
(* {"{{52.000,52.289,39.000,59.000 .... *)

It is a List with a single element with a Head of String. So take the first element and convert from string to expression.

data = Import["~/Downloads/tESTmATRIX.txt", "List"] // First // ToExpression;
InputForm@data
(* {{52., 52.289, 39., 59., .... *)
POSTED BY: Rohit Namjoshi
Posted 5 years ago

I have uploaded a small sample file. My algorithm does not work when the file is imported with 'List', 'Table', 'Data', etc but it does work if I copy the contents of the file and paste them into Mathematica.

Attachments:
POSTED BY: Jamie Dixson
Posted 5 years ago

Hi Jamie,

It would help if you could attach a small sample of the file. Try "Table" or "List" instead of "Data".

POSTED BY: Rohit Namjoshi
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