Mathematica:导出到变量路径

发布于 2024-11-07 07:25:58 字数 152 浏览 0 评论 0原文

这很棘手。 一旦 Mathematica 中导出数据的路径被引号括起来,如何插入变量作为路径的一部分?换句话说,我处于一个递增 VAL 的循环中,并且想要将 MyData 导出到 VAL.dat。有想法吗?

伪代码: 导出[“~/Documents/VAL”,MyData]

this is tricky.
Once the path to export data in Mathematica is under quotes, how can I insert a variable as part of the path? In other words, I'm inside a loop that increments VAL and want to export MyData to VAL.dat. Ideas?

Pseudocode:
Export["~/Documents/VAL", MyData]

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

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

发布评论

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

评论(3

肥爪爪 2024-11-14 07:25:58

除了 Howard 和 Mr.Wizard 的回答之外,我可以说,最好查找 FileNameJoin 来寻找一种很好的、​​独立于系统的方式来组成路径字符串和 IntegerString ,其中您可以使用将整数转换为具有固定位置数的字符串,使您的文件排序得更好:

In[33]:= VAL = 32;
IntegerString[VAL, 10, 4]

Out[34]= "0032"

我通常不太需要操作系统间兼容性(主要为我自己编程),所以我通常的风格是这样的

Export["directoryPart\\FixedFileNamePart"<>IntegerString[VAL, 10, 4]<>".dat","TSV"]

代替如果扩展名不清楚,请使用您需要的文件类型“TSV”。请注意,我在 Windows 上,它使用反斜杠作为分隔符。由于这也是转义字符,因此必须使用反斜杠本身进行转义;这解释了双反斜杠。您似乎使用的是 UNIX 衍生版本,因此不需要这样做。这确实显示了 FileNameJoin 的值,它会自动处理这些详细信息。

In addition to Howard and Mr.Wizard's answers I could say that it would be good to look up FileNameJoin for a nice, system-independent way to compose path strings and IntegerString which you could use to convert integers to strings with a fixed number of positions, making your files sort more nicely:

In[33]:= VAL = 32;
IntegerString[VAL, 10, 4]

Out[34]= "0032"

I usually don't have much need for inter-OS compatibility (programming mostly for myself), so my usual style would be something like

Export["directoryPart\\FixedFileNamePart"<>IntegerString[VAL, 10, 4]<>".dat","TSV"]

Replace "TSV" with the file type you need if it isn't clear from the extension. Please note that I am on windows, which uses the backslash as separator. Since this is also the escape character, it has to be escaped with a backslash itself; this explains the double backslash. You seem to be on a UNIX derivate so there's no need for that. This does show the value of FileNameJoin which takes care of these details automatically.

意中人 2024-11-14 07:25:58

如何将您的数字转换为字符串并将其与路径连接:

"~/Documents/"<>ToString[VAL]

How about converting your number to string and join it with the path:

"~/Documents/"<>ToString[VAL]
带刺的爱情 2024-11-14 07:25:58

在直接回答您的问题时,您可以使用StringReplace

Table[
  StringReplace[
     "~/Documents/#.dat", 
     "#" :> IntegerString[VAL, 10, 4]],
  {VAL, 27, 29}
]
   {"~/Documents/0027.dat", "~/Documents/0028.dat", "~/Documents/0029.dat"}

“#”被任意选择作为占位符。也可以使用其他字符或字符串。

In direct answer to your question, you can use StringReplace:

Table[
  StringReplace[
     "~/Documents/#.dat", 
     "#" :> IntegerString[VAL, 10, 4]],
  {VAL, 27, 29}
]
   {"~/Documents/0027.dat", "~/Documents/0028.dat", "~/Documents/0029.dat"}

"#" was arbitrarily chosen as a placeholder. Another character or string of characters could be used just as well.

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