如何在 Mathematica 中将文件中的数据导入为列表/数组

发布于 2024-08-15 20:03:49 字数 935 浏览 1 评论 0原文

我有必须从另一个程序导入到 Mathematica 的数据(点),这样我就可以绘制它。我可以控制如何将点写入文件,因此我可以按照我想要的任何方式放置它们。将它们导入 Mathematica 的最佳方法是什么?由于我将使用 StreamDensityPlot,因此我必须传递给 StreamDensityPlot 的变量必须采用以下方式:

data = {
        {
            { a, b, c }, {a, b, c}, {a, b, c},
            { a, b, c }, {a, b, c}, {a, b, c},
            { a, b, c }, {a, b, c}, {a, b, c},
        }
    ...
        {
            { a, b, c }, {a, b, c}, {a, b, c},
            { a, b, c }, {a, b, c}, {a, b, c},
            { a, b, c }, {a, b, c}, {a, b, c},
        }
    }

您建议我如何将数据放入中间文本文件中?我应该用什么来导入它?我尝试过 Import["mytext.txt", "List"],让我的文本文件包含上面所示形式的内容,但似乎 Mathematica 将这些点视为字符串,我无法对它们执行任何操作。有没有办法像其他语言一样将字符串转换为任意数据(前提是它们在该新数据类型中有效)?

回顾:

  1. 是否可以在 Mathematica 中将字符串(例如“5”)转换为数字?如果是,怎么办?
  2. 是否可以将像“{1,2,3}”这样的字符串转换为 Mathematica 中的列表?如果是,怎么办?
  3. 是否可以将 CSV 文件作为列表列表加载(如 Mathematica 中所示)?

谢谢

I have data(points) that I have to import from another program to Mathematica, so I can plot it. I have control on how the points are going to be written to the file, so I can put them in any way I want. What is the best way to import them to Mathematica? As I'm going to use StreamDensityPlot, the variable I'll have to pass to StreamDensityPlot will have to be in the following way:

data = {
        {
            { a, b, c }, {a, b, c}, {a, b, c},
            { a, b, c }, {a, b, c}, {a, b, c},
            { a, b, c }, {a, b, c}, {a, b, c},
        }
    ...
        {
            { a, b, c }, {a, b, c}, {a, b, c},
            { a, b, c }, {a, b, c}, {a, b, c},
            { a, b, c }, {a, b, c}, {a, b, c},
        }
    }

How would you advice me to put the data in the intermediate text file? And what should I use to import it? I've tried Import["mytext.txt", "List"], having my text file with something in the form shown above but it seems as Mathematica considers the points as strings, and I can't do anything with them. Is there a way to convert strings to arbitrary data as is possible in other languages (provided they are valid in that new data type)?

Recap:

  1. Is it possible to convert a string, for example, "5" to a number, in Mathematica? If yes, how?
  2. Is it possible to convert a string like "{1 , 2, 3}" to a list in Mathematica? If yes, how?
  3. Is it possible to load a CSV file as a list of lists as shown above in Mathematica?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

末蓝 2024-08-22 20:03:49

将字符串转换为表达式是通过 ToExpression 完成的。如果您有一个纯文本文件 foo.txt,其格式如示例中所示,则只需使用 获取,即<< /path/to/foo.txt; 将以您想要的方式导入和评估数据,无需文本到表达式的翻译。

Converting Strings to Expressions is done with ToExpression. If you have a plain text file foo.txt, with the formatting as in your example, then just importing it into Mathematica with Get, i.e., << /path/to/foo.txt; will import and evaluate data in the way you want, no need for text to expression translations.

机场等船 2024-08-22 20:03:49

尝试像这样格式化您的数据文件:

A, B, C, A, B, C, A, B, C
A, B, C, A, B, C, A, B, C
A, B, C, A, B, C, A, B, C
...

这样您就可以使用 Mathematica 的 CSV 导入。然后在导入后将每一行划分为点列表。

Partition[#, 3]& /@ Import["file.csv", "CSV"]

另外,请记住,Mathematica 的科学记数法与 C(或您用来编写数据文件的任何语言)不同。

Try formatting your data file like this:

A, B, C, A, B, C, A, B, C
A, B, C, A, B, C, A, B, C
A, B, C, A, B, C, A, B, C
...

So you can use Mathematica's CSV import. Then partition each row into a list of points after import.

Partition[#, 3]& /@ Import["file.csv", "CSV"]

Also, keep in mind that Mathematica does scientific notation differently than C (or whatever language you're using to write the data file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文