2012-07-24 61 views
1

我正在使用包含Mathematica中的y坐标集的.raw文件绘制图。我不确定要输入的内容直接引用文件中的数据 - 我目前使用的是“数据”,但不确定这是否正确。从Mathematica中的.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. >> 

任何帮助,将不胜感激。

+0

你能给出一组“数据”中前几项的样本吗? – 2012-07-24 14:24:49

+0

例如一个是: 702.00000 704.00000 706.00000 708.00000 .... 并持续10000分的结果。 – 2012-07-24 14:30:33

+0

所以它看起来像'data = {702.00000,704.00000,706.00000,708.00000}'或'data =“702.00000 704.00000 706.00000 708.00000”'? – 2012-07-24 14:43:56

回答

1

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

data = "702.00000 704.00000 706.00000 708.00000" 

这可通过观察其输出StringHead[data]看到。

解析它,只需使用

[email protected]@data 

,而不是dataListPlot

ListPlot[[email protected]@data, Frame -> True, PlotRange -> {0, 2000}, 
DataRange -> {0, 10000}, AxesOrigin -> {0, 0}] 
从您的代码

Mathematica graphics

3

的一个问题是,你永远不会得到周围分配可变'数据'。你可能意思是

data = ReadList[filename, Number]; 

第二个问题是ReadList。这是相当古老的,尽管它可以工作,并且比导入速度快大约10倍。因为您正在读取的数字为Number(...),您不需要将它们转换为字符串。第三个问题是AppendTo。这个命令非常慢。我建议采用索引方法。类似于

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

应该就足够了。