利用MATLAB对数据进行切片并绘制图表
1 读取txt文件
使用MATLAB中的load函数加载记事本文件,使用size函数获取行数和列数
filename = '2019-03-24 10_04_47-iip-header.txt';
data = load(filename);
[Rows, Columuns] = size(data); %如果是仅仅获取行数,则使用size(data,1)
在读取的时候,load函数出现了错误,原因是load函数只能读取完全为数字的文本,而打开这个文件发现最后一行数据稍微有点缺失:

因此改为importdata函数
data = importdata(filename);
2 对矩阵进行切片,提取信息
从header.txt文件中读取数据,主要读取以下信息:
| 序号 | 含义 | 变量命名 |
|---|---|---|
| 31 | 经度 | longitude |
| 32 | 维度 | latitude |
| 96 | 期望速度 | expectedVelocity (Surge) |
| 78 | 期望艏向角 | expectedHeading |
| 43 | 实际输出速度 | outputVelocity (Surge) |
| 34 | 实际输出艏向角 | outputHeading |
| 35 | 实际输出艏向角角速率 | outputYaw |
把需要的数据读取成列向量
根据接口协议,把需要的数据进行切片,得到列向量
longi = data( : , 31);
lati = data( : , 32);
expectedVelocity = data( : ,96);
expectedHeading = data( : ,78);
outputVelocity = data( : ,43);
outputHeading = data( : ,34);
3 使用MATLAB绘图
Matlab中使用figure和plot函数绘图,期望值用绿色表示,实际值用红色表示
figure
plot(expectedVelocity,'g','linewidth',2);
hold on
plot(outputVelocity,'r','linewidth',2);
legend('expectedVelocity','outputVelocity');
figure
plot(expectedHeading,'g','linewidth',2);
hold on
plot(outputHeading,'r','linewidth',2);
legend('expectedHeading','outputHeading');
得到图形

4 附录——完整代码
clc;
clear all;
close all;
filename = '2019-03-24 10_04_47-iip-header.txt';
% load只能用于仅含有数字的文本文件
% data = load(filename); %最后一行数据不太对,该用textread
% data = textread(filename,'emptyvalue',NaN)
data = importdata(filename);
longi = data( : , 31);
lati = data( : , 32);
expectedVelocity = data( : ,96);
expectedHeading = data( : ,78);
outputVelocity = data( : ,43);
outputHeading = data( : ,34);
figure
plot(expectedVelocity,'g','linewidth',2);
hold on
plot(outputVelocity,'r','linewidth',2);
legend('expectedVelocity','outputVelocity');
figure
plot(expectedHeading,'g','linewidth',2);
hold on
plot(outputHeading,'r','linewidth',2);
legend('expectedHeading','outputHeading');
672




被折叠的 条评论
为什么被折叠?



