1. 程式人生 > >c++儲存和讀取資料到txt檔案中並用python畫圖

c++儲存和讀取資料到txt檔案中並用python畫圖

#include<fstream>
#include<iostream>
using namespace std;
int main()
{
	ofstream out("D://wang//test.txt");//寫資料到指定目標資料夾,這個資料夾裡可以沒有test.txt檔案,
	if(out.is_open())
	{
		out<<"this is a line.\n";
		out<<"This is another lien.\n";
		out.close();
	}
	//從test.txt中讀取資料
	char buffer[256];
	ifstream in("D://wang//test.txt");
	if (!in.is_open())
	{
		cout<<"Error opening file";
	}
	while (!in.eof())
	{
		in.getline(buffer,100);
		cout<<buffer<<endl;
	}
	return 0;

實際應用

SmoothPath(std::vector<geometry_msgs::PoseStamped> &total_plan)

    {

	    
	     ofstream out1("/home/viewbot/wanglinearV5.txt",ios::app);
	    
	    int total_plan_size = total_plan.size();	    

	    double sum_x,sum_y;	
	    int d,window_0,window_1;

	    for (int i = 0; i <= total_plan_size; i++)

	    {

		    
	        double x1=total_plan[i].pose.position.x; 
	        double y1=total_plan[i].pose.position.y;
	          
		    sum_x = 0.0;

		    sum_y = 0.0;
		    d = 20;
            d = std::min(i, d);
            d = std::min(total_plan_size-i-1, d);
            window_0 = i-d;
            window_1 = i+d;		

		    for (int j = window_0; j < window_1+1; j++)

		    {

			    sum_x += total_plan[j].pose.position.x;

			    sum_y += total_plan[j].pose.position.y;

		    }

		    total_plan[i].pose.position.x = sum_x / (2*d+1);

		    total_plan[i].pose.position.y = sum_y / (2*d+1);
		     
	        out1<<x1<<' '<<y1<<' '<<total_plan[i].pose.position.x<<' '<<total_plan[i].pose.position.y<<endl; 

	    }	
	      out1.close();
	        

   }

畫圖


import matplotlib.pyplot as plt
filename = 'wanglinearV5.txt'
#filename = 'normalfootprint.txt'
x1,y1,x2,y2,e1,e2= [],[],[],[],[],[]


i=0


with open(filename, 'r') as f:#1
    lines = f.readlines()#2
    for line in lines:#3
        value = [float(s) for s in line.split(" ")]#4
        x1.append(value[0])
        y1.append(value[1])
        x2.append(value[2])
        y2.append(value[3])
        e1.append(value[0]-value[2])
        e2.append(value[1]-value[3])
        
        
       

def velotu():
    #line1=plt.scatter(x1, y1)
    line2=plt.plot(x1, y1)
    #line3=plt.scatter(x2, y2)
    line4=plt.plot(x2, y2)
    line5=plt.plot(e1, e2)
    plt.legend([line2, line4,line5], ["error_vel","error_ang","yy"], loc=1)
    
    plt.show()

if __name__ == '__main__':
 
    velotu()
    

平滑效果

相關推薦

c++儲存讀取資料到txt檔案並用python畫圖

#include<fstream> #include<iostream> using namespace std; int main() { ofstream out("D://wang//test.txt");//寫資料到指定目標資料夾,這個資料夾

c# 去掉檔案讀取txt檔案的空格

百度沒有查到,試驗出一個簡單的方法如下dr[0] = data[0].Trim();//去除字串中的空格private void button1_Click(object sender, EventArgs e) { //建立一個開啟檔

安卓按鍵的回車換行符讀取寫入txt檔案的注意事項

我們用安卓按鍵寫指令碼  有一個地方經常容易困惑  比如我明明在電腦上寫好的txt檔案 轉移到手機上開啟 就會全部內容都在一個整行   會造成一些讀取寫入和檢視的不方便  糟糕的時候會導致讀取寫入失敗

c++逐行讀取寫入txt檔案的方法

檔案開啟模式: ios::in            讀 ios::out           寫 ios::app           從檔案末尾開始寫 ios::binary        二進位制模式 ios::nocreate      開啟一個檔案時,如果檔案不存在,不建立檔案。 ios::n

QTVS讀取.txt檔案資料儲存為陣列

QT中讀取.txt檔案並儲存為陣列 QFile fpx("/work/qt_workspace/saly/pix_x.txt"); QFile fpy("/work/qt_workspace/saly/pix_y.txt"); // QVector&l

json-c 的安裝使用(使用json-c讀取檔案的json資料儲存json格式的資料檔案

1、json資料格式和json-c簡介[1] JSON(JavaScriptObject Notation) 是一種輕量級的資料交換格式。易於人閱讀和編寫。同時也易於機器解析和生成。 JSON採用完全獨立於語言的文字格式,但是也使用了類似於C語言家族的習慣(包括C,C++

MATLAB批量從txt檔案讀取指定行的資料儲存txt檔案

經常遇到,我們想要讀取的資料,不是從頭開始讀取的,我們往往要求從固定的行開始讀取,MATLAB程式碼如下: function readData() %從指定行開始讀取資料 readFilePat

Java讀取介面資料,並儲存txt檔案

//建立讀取介面中資料的方法 public static String read() { URL url = null; BufferedReader reader = null; HttpURLConnection connection = null; I

【matlab】Matlab實現資料以.mat檔案形式的儲存讀取

1、儲存 利用save函式 save(filename)  %將當前工作區中的所有變數儲存在 MATLAB® 格式的二進位制檔案(MAT 檔案)filename 中。 save(filename,variables) %僅儲存 variables 指定的結構體陣列的變數

Java 讀取excel指定行列資料以及將資料儲存txt檔案

在使用的軟體中經常要用到一些資料的匯入匯出,以及準確的定位資料,這些經常會涉及excle表格,因此把今天學習到的關於如何利用Java準確獲取到excle中的某一列資料,同時將此列資料輸出到txt檔案格式中。 使用的jar包:jxl.jar  相關的API:http://jx

Pythontxt檔案逐行讀取資料

Python從txt檔案中逐行讀取資料   # -*-coding:utf-8-*- import os for line in open("./samples/label_val.txt"): print('line=', line, end = '') #後面

python操作txt檔案資料教程[3]-python讀取資料所有txt檔案並將資料轉為csv檔案

python操作txt檔案中資料教程[3]-python讀取資料夾中所有txt檔案並將資料轉為csv檔案 覺得有用的話,歡迎一起討論相互學習~Follow Me 參考文獻 python操作txt檔案中資料教程[1]-使用python讀寫txt檔案 python操作txt檔案中資料教程[2]-pyth

c語言學生成績管理系統(可以將學生資訊儲存txt檔案

程式截圖:  標頭檔案說明; 定義全域性變數;   定義、編寫輸入函式; 定義、編寫顯示函式; 定義、編寫修改函式; 定義、編寫查詢函式; 定義、編寫新增函式; 定義、編寫排序函式; 定義、編寫刪除函式; 定義、編

Mongodb學習筆記三、使用asp.net在Mongodb儲存讀取圖片檔案

今天練習瞭如何使用c# driver儲存和讀取圖片。 廢話不多說,直接上程式碼。 一、儲存圖片(檔案應該也一樣): private void SaveImgBJSON(string id, byte[] byteImg) {

Java實現在文字檔案寫入資料讀取資料

Java實現在文字檔案中寫入資料和讀取資料 寫資料到文字檔案中去: 程式碼如下所示: import java.io.File; import java.io.FileWriter; import java.io.IOException; public class

pythontxt檔案資料時的追加覆蓋問題

  最近在用python從WOS網站中爬取內容並儲存到本地的txt檔案中,發現每次寫入都是把txt檔案中原來存在的內容覆蓋掉了,那麼如何才能在原來的基礎上繼續往裡面新增內容呢? 1、原來的開啟檔案的方式是: file = open(pathTxt, 'w', encoding

PHP 從txt檔案讀取資料

一、開啟/關閉檔案 1、對檔案操作時首先要開啟檔案,開啟檔案用 fopen()函式,語法是: fopen(filename,mode,include_path,context); 2、對檔案操作結束後應該關閉這個檔案,使用函式 fclose(); 例如: 二、讀寫檔案 1、讀取整個檔案,有

讀取檔案全部內容---C++ 寫入資料檔案

//使用char接收---#include<fstream> #include<fstream> std::ifstream t; int length = 0; t.open("../vtkWriter.cxx"); // open

Matlab讀取txt檔案資料(使用textread函式)

在使用Matlab處理資料時,我們經常需要讀取txt文件,可以使用Matlab中強大的textread函式。它的基本語法是:[A,B,C,...] = textread(filename,format)[A,B,C,...] = textread(filename,forma

javahashtable怎樣儲存資料讀取資料

Hashtable-雜湊表類 以雜湊表的形式儲存資料,資料的形式是鍵值對. 特點: 查詢速度快,遍歷相對慢 鍵值不能有空指標和重複資料 建立Hashtable<Integer,String> ht=new Hashtable<Integer,String>(); 添值 ht.put(