0
$\begingroup$

Although this question was kind of already asked, it was closed because not enough information was given. Here is my attempt. I am trying to find out the correct format to import a vector data set (this will be an output from c and obviously I can put it in any format I want). The problem is a list vector data set from Mathematica is in a very interesting format, i.e.

    data = Table[{-1 - x^2 + y, 1 + x - y^2}, {x, -3, 3, 1}, {y, -3, 3, 1}]

will give something that looks like,

    {{{-13,-11},....},{{-8,-10}...}}

In trying to work backwards, I tried exporting the above table as

    Export["test_vector.txt", data, "csv"]

then import it again as

    a = Import["test_vector.txt", "csv"]

In looking at "a" compared to "data" it looks exactly the same except for a space between the vector components, i.e.

    {{{-13, -11},....},{{-8, -10}...}}

I am able to

    ListStreamPlot[data]

but when I go to do the same for "a" I get multiple errors, (Prat 1 of {} does not exist, Recursion depth of 1024 exceeded) and the statement that it is not a valid vector field dataset. (ListVectorPlot/ListStreamPlot; whichever, they both require the same type of dataset.)

Once again my question is, in what format do I need to put my data, so that I can import it into Mathematica as a vector dataset.

$\endgroup$
2
$\begingroup$

Whenever Mathematica does something which looks funny or you can't understand why it is behaving differently, the first thing to do is to look at the InputForm or FullForm of the expression. In this case:

In[7]:= a // InputForm
Out[7]//InputForm= {{"{-13, -11}", "{-12, -6}", ...

So you see you have a matrix of strings, not a rank-three array of numbers. This is because CSV is a matrix/spreadsheet format, so individual entries which are not simple numbers are convertered to strings.

My guess is that you CSV is the wrong format for you. You probably want to use Put and Get:

In[36]:= data >> test_vector.txt
In[37]:= Get["test_vector.txt"] // InputForm
Out[37]//InputForm= {{{-13, -11}, {-12, -6}, ...

Now you have really numbers you can use. If you really want CSV, you'll need to fix it up after import by Maping ToExpression at the right level:

In[42]:= Export["test_vector.txt", data, "CSV"]
In[43]:= Map[ToExpression, Import["test_vector.txt", "CSV"], {2}] === data
Out[43]= True
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.