Mathematica:导出到变量路径
这很棘手。 一旦 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了 Howard 和 Mr.Wizard 的回答之外,我可以说,最好查找
FileNameJoin
来寻找一种很好的、独立于系统的方式来组成路径字符串和IntegerString
,其中您可以使用将整数转换为具有固定位置数的字符串,使您的文件排序得更好:我通常不太需要操作系统间兼容性(主要为我自己编程),所以我通常的风格是这样的
代替如果扩展名不清楚,请使用您需要的文件类型“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 andIntegerString
which you could use to convert integers to strings with a fixed number of positions, making your files sort more nicely:I usually don't have much need for inter-OS compatibility (programming mostly for myself), so my usual style would be something like
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.如何将您的数字转换为字符串并将其与路径连接:
How about converting your number to string and join it with the path:
在直接回答您的问题时,您可以使用
StringReplace
:“#”被任意选择作为占位符。也可以使用其他字符或字符串。
In direct answer to your question, you can use
StringReplace
:"#" was arbitrarily chosen as a placeholder. Another character or string of characters could be used just as well.