如何在 Mathematica 中将文件中的数据导入为列表/数组
我有必须从另一个程序导入到 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 将这些点视为字符串,我无法对它们执行任何操作。有没有办法像其他语言一样将字符串转换为任意数据(前提是它们在该新数据类型中有效)?
回顾:
- 是否可以在 Mathematica 中将字符串(例如“5”)转换为数字?如果是,怎么办?
- 是否可以将像“{1,2,3}”这样的字符串转换为 Mathematica 中的列表?如果是,怎么办?
- 是否可以将 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:
- Is it possible to convert a string, for example, "5" to a number, in Mathematica? If yes, how?
- Is it possible to convert a string like "{1 , 2, 3}" to a list in Mathematica? If yes, how?
- Is it possible to load a CSV file as a list of lists as shown above in Mathematica?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将字符串转换为表达式是通过 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 evaluatedata
in the way you want, no need for text to expression translations.尝试像这样格式化您的数据文件:
这样您就可以使用 Mathematica 的 CSV 导入。然后在导入后将每一行划分为点列表。
另外,请记住,Mathematica 的科学记数法与 C(或您用来编写数据文件的任何语言)不同。
Try formatting your data file like this:
So you can use Mathematica's CSV import. Then partition each row into a list of points after import.
Also, keep in mind that Mathematica does scientific notation differently than C (or whatever language you're using to write the data file.