2013-10-17 3 views
2

나는 자바와 함께 gnuplot을 사용하고 있으며, 나를 미치게 만든이 문제가있다. 기본적으로,이 같은 음모에 두 개의 더블 [] 배열을 플롯이 기능을 사용하고 있습니다 -자바와 함께 gnuplot 사용

public static void plot(String filename,double[] ua1,double[] ua2) throws IOException{ 
    if(ua1.length==0 | ua2.length==0){ 
     System.out.println("This one had no data - " + filename); 
     return; 
    } 
    File fold1 = new File("old"); 
    if(fold1.exists()){ 
     boolean a = fold1.delete(); 
     if(!a)System.out.println("Houstoonnn!!!"); 
    } 
    fold1 = new File("new"); 
    if(fold1.exists()){ 
     boolean a = fold1.delete(); 
     if(!a)System.out.println("Not deleted!!!"); 
    } 
    FileWriter outF1 = new FileWriter("old"); 
    FileWriter outF2 = new FileWriter("new"); 
    PrintWriter out1 = new PrintWriter(outF1); 
    PrintWriter out2 = new PrintWriter(outF2); 
    for(int j=0;j < ua1.length;j++){ 
     out1.println(ua1[j]); 
     out2.println(ua2[j]); 
    } 
    out1.close(); 
    out2.close(); 
    File fold2 = new File("auxfile.gp"); 
    try{//If the file already exists, delete it.. 
     fold2.delete(); 
    } 
    catch(Exception e){} 
    FileWriter outF = new FileWriter("auxfile.gp"); 
    PrintWriter out = new PrintWriter(outF); 
    out.println("set terminal gif"); 
    out.println("set output \""+ filename+".gif\""); 
    out.print("set title " + "\""+filename+"\"" + "\n"); 
    out.print("set xlabel " + "\"Time\"" + "\n"); 
    out.print("set ylabel " + "\"UA\"" + "\n"); 
    out.println("set key right bottom"); 
    out.println("plot \"old\" with linespoints,\"new\" with linespoints"); 
    out.close();// It's done, closing document. 
    Runtime.getRuntime().exec("gnuplot auxfile.gp"); 
} 

아이디어는 파일을 분리의 gnuplot으로 그들을 음모를 모두 더블을 작성하는 것입니다. 이 함수를 한 번 호출하면 잘 작동합니다. 그러나 루프에서 반복적으로 호출 할 때 생성되는 빈 파일과 잘못된 파일 (예를 들어 줄을 알 수없는 동안 줄이 몇 번 줄어든 경우)이 표시됩니다. 어떤 경우에는 제대로 작동하기 때문에 매우 무작위입니다. gnuplot을 호출하기 전에 파일을 읽고 쓰는 것과 관련이 있다는 것을 알고 있습니다. 어떤 사람이이 플로팅 기능을 향상시켜이 이상한 행동을 보지 못하게 할 수 있습니까?

+1

일종의 경쟁 조건 일 수 있습니다. [Java : exec 프로세스가 끝날 때까지 기다리십시오] (http://stackoverflow.com/q/12448882/2604213)를 참조하십시오. – Christoph

+0

감사합니다 Christoph. 이것은 정확히 문제였습니다. 이 함수를 호출 할 때마다 Thread.sleep (10)을 추가했는데 문제가 해결 된 것 같습니다. –

답변

1

당신은 JavaGnuplotHybrid을 시도 할 수 있습니다 :

다음

더블 [] 배열을 플로팅 코드입니다 :

public void plot2d() { 
    JGnuplot jg = new JGnuplot(); 
    Plot plot = new Plot("") { 
     { 
      xlabel = "x"; 
      ylabel = "y"; 
     } 
    }; 
    double[] x = { 1, 2, 3, 4, 5 }, y1 = { 2, 4, 6, 8, 10 }, y2 = { 3, 6, 9, 12, 15 }; 
    DataTableSet dts = plot.addNewDataTableSet("2D Plot"); 
    dts.addNewDataTable("y=2x", x, y1); 
    dts.addNewDataTable("y=3x", x, y2); 
    jg.execute(plot, jg.plot2d); 
} 

enter image description here

그것은 매우 간단하고 간단합니다. 더 자세한 내용은 https://github.com/mleoking/JavaGnuplotHybrid

관련 문제