1

我正在尝试使用包含 Mathematica 中的 y 坐标集的 .raw 文件绘制图形。我不确定要键入什么内容才能直接直接引用文件中的数据——我目前正在使用“数据”,但不确定这是否正确。

这是我的代码:

    SetDirectory[$HomeDirectory <> "/Documents/Project/Work/Output"]
    alldirs = FileNames["deBB-*"]
    alllocdata = {};
    Do[
       SetDirectory["./" <> alldirs[[idir]]];
       Print["--- working on " <> (dirname = alldirs[[idir]])];
       allfiles = FileNames["T-*.raw"];
       Do[
          Print["   --- working on " <> (filename = allfiles[[ifile]])];
          ReadList[filename, Number];
          AppendTo[alllocdata, data];
          Print[ListPlot[data, Frame -> True, PlotRange -> {0, 2000}, 
          DataRange -> {0, 10000},
          AxesOrigin -> {0, 0}]], {ifile, Length[allfiles]}
       ];
       SetDirectory[ParentDirectory[]],
       {idir, Length[alldirs]}
    ]

我一直收到这个错误:

    ListPlot::lpn: data is not a list of numbers or pairs of numbers. >>

任何帮助,将不胜感激。

4

2 回答 2

3

您的代码中的一个问题是您从未分配过变量“数据”。你的意思可能是

data = ReadList[filename, Number];

第二个问题是ReadList。这是相当老派的,虽然它可以工作,并且(大约 10 倍)比 Import 快。因为您将数字读取为Number(...),所以您不需要将它们转换为字符串。

第三个问题是AppendTo。这个命令是出了名的慢。我建议采用索引方法。就像是

basdir = "~/parentdir";
SetDirectory[basdir];
alldir = FileNames["deBB-*"];
alldir = Select[alldir, DirectoryQ[#] &] (* directories only *);
alldat = Range[Length@alldir];
(
    SetDirectory[cdir = StringJoin[basdir, "/", alldir[[#]]]];
    Print["Working in ", cdir];
    allfil = FileNames["T-*.raw"];
    alldat[[#]] = Range[Length@allfil];
    tmpdat = Import[allfil[[#]], "Table"] & /@ Range[Length@allfil];
    alldat[[#]] = tmpdat;
    Print@ListPlot[tmpdat[[#]], PlotLabel -> allfil[[#]]] & /@ 
     Range[Length@allfil];
    ) & /@ Range[Length@alldir];

应该足够了。

于 2012-07-24T23:39:54.673 回答
1

根据您的评论, thedata实际上是一个String,而不是一个数字量。

data = "702.00000 704.00000 706.00000 708.00000"

这可以通过查看Head[data]which 输出来看出String

要解析它,只需使用

ToExpression@StringSplit@data

而不是data在你的ListPlot

ListPlot[ToExpression@StringSplit@data, Frame -> True, PlotRange -> {0, 2000}, 
 DataRange -> {0, 10000}, AxesOrigin -> {0, 0}]

数学图形

于 2012-07-24T15:29:45.300 回答