1、gps ,数值格式的读取

clear all
test=importdata('2017- 9-27- 8-26-51.txt');
[r,c]=size(test.data);%row行,column列
a=max(size(test.data));%行
l=length(test.data);
for i=1:r
lon(i)=test.data(i,8);%经度
lat(i)=test.data(i,7);
end
%% 异常处理
lon(find(lon==0))=[];
lat(find(lat==0))=[];
plot(lon,lat);
find(lon==0)
xlabel('经度','fontsize',13,'fontweight','bold');
ylabel('纬度');
title('轨迹')
grid on
save gps_27 lon lat
% 航迹
close all
clear
clc
load gps_27.mat
%%
long_rang = [ 121 125];%[112.7780 114.4754]
lat_rang = [ 29 30.5 ]; % [20.8469 22.4512]
% load coast
m_proj('lambert','long',long_rang,'lat',lat_rang);
m_gshhs_i('patch',[.7 1 .7],'edgecolor','k');
% High Resolution Coastline...
m_grid('linestyle','none','box','fancy','tickdir','in','fontsize',8);
m_line(lon,lat,'linewi',1.5,'color','r');
title('航迹图','fontsize',14);
print(gcf,'-djpeg','-r600','航迹图.jpg');

2、指针末尾,按行读取

(1)

clear all
test=importdata('2017- 9-27- 9-34- 1-server.txt');

cell结构不能用importdata读取。 

(2)

clear all
fid = fopen('1.txt'); k = 0;
while feof(fid)~=1 %到末尾返回1,中间位置返回0,判断文件读取是否完成的循环
curr = fscanf(fid,'%c',1);
% curr = fgetl(fid);
if ~isempty(curr)
k = k+1;
benchstr(k) = curr;
end
end fclose(fid);

(2)

clear all
fid = fopen('1.txt');%打开现有文档
fid_new=fopen('scr_new.txt','wt');%新创建存储的文档
k = 0;
while feof(fid)~=1
curr = fgetl(fid);
if ~isempty(curr)
fprintf(fid_new,'%s\n',curr);
end
end fclose(fid_new);

(3)

clear all
% fileID = fopen('C:\Users\Administrator\Desktop\2017- 9-29- 7- 5-16.txt','r');
fid=fopen('scr_new_2.txt','wt'); %新建一个txt文件
phns = ['C:\Users\Administrator\Desktop\舟山_9\波导气象数据\2017- 9-29- 7- 5-23.txt']; %要读取的文档所在的路径
fpn = fopen (phns, 'rt'); %打开文档
while feof(fpn) ~= 1 %用于判断文件指针p在其所指的文件中的位置,如果到文件末,函数返回1,否则返回0 file = fgetl(fpn); %获取文档第一行
new_str=file;
fprintf(fid,'%s\n',new_str);%新的字符串写入当新建的txt文档中
end
fclose(fid);

按行读取

(4)保存数据(cell)格式 

clear all
% fileID = fopen('C:\Users\Administrator\Desktop\舟山_9\波导气象数据\2017- 9-29- 7- 5-16.txt','r');
fid=fopen('scr_new_2.txt','wt'); %新建一个txt文件
phns = ['C:\Users\Administrator\Desktop\舟山_9\波导气象数据\2017- 9-29- 7- 5-23.txt']; %要读取的文档所在的路径
fpn = fopen (phns, 'rt'); %打开文档
count = 1;
wea_27={}
fgetl(fpn);
while feof(fpn) ~= 1 %用于判断文件指针p在其所指的文件中的位置,如果到文件末,函数返回1,否则返回0
temp = fgetl(fpn);
flag = 0;
flag = 1;
index = find(temp == ' ');
date(count,1)=str2double(temp(1:index(1)-1));
date(count,2)=str2double(temp(index(3)+1:index(4)-1));
date(count,3)=str2double(temp(index(5)+1:index(6)-1));
date(count,4)=str2double(temp(index(7)+1:index(8)-1));
date(count,5)=str2double(temp(index(9)+1:index(10)-1));
date(count,6)=str2double(temp(index(11)+1:index(12)-1));%年月日时分秒向量表示 wea_27{count,1} = datestr(date(count,:),31);%时间
wea_27{count,2} = str2double(temp(index(17)+1:index(18)-1));%经度
wea_27{count,3} = str2double(temp(index(15)+1:index(16)-1));%纬度
wea_27{count,4} = str2double(temp(index(28)+1:end));%波导高度 count = count+1;
fprintf(fid,'%s %f %f %f \n',wea_27{1,:});
end
fclose(fid);
save wea_27 wea_27

转换时间格式,将经纬度提取出来。

(5)提取数据

clear all
% fileID = fopen('C:\Users\Administrator\Desktop\舟山_9\波导气象数据\2017- 9-29- 7- 5-16.txt','r');
fid=fopen('scr_new_2.txt','wt'); %新建一个txt文件
phns = ['C:\Users\Administrator\Desktop\舟山_9\波导气象数据\2017- 9-29- 7- 5-23.txt']; %要读取的文档所在的路径
fpn = fopen (phns, 'rt'); %打开文档
count = 1;
fgetl(fpn);
while feof(fpn) ~= 1 %用于判断文件指针p在其所指的文件中的位置,如果到文件末,函数返回1,否则返回0
temp = fgetl(fpn);
flag = 0;
flag = 1;
index = find(temp == ' ');
date(count,1)=str2double(temp(1:index(1)-1));
date(count,2)=str2double(temp(index(3)+1:index(4)-1));
date(count,3)=str2double(temp(index(5)+1:index(6)-1));
date(count,4)=str2double(temp(index(7)+1:index(8)-1));
date(count,5)=str2double(temp(index(9)+1:index(10)-1));
date(count,6)=str2double(temp(index(11)+1:index(12)-1));%年月日时分秒向量表示
% data_27(count) = datestr(date(count,:),31);%时间
wea_27(count,2) = str2double(temp(index(17)+1:index(18)-1));%经度
wea_27(count,3) = str2double(temp(index(15)+1:index(16)-1));%纬度
wea_27(count,4) = str2double(temp(index(28)+1:end));%波导高度
count = count+1;
end
wea_27(find(wea_27(:,2)==0),:)=[];%去除异常数据
a=wea_27(:,2)';
b=wea_27(:,3)';
plot(a,b)

(6)去除NaN

clear
close all
clc
%%
%% 9030A
filename = ['1_27_Sep_2017_11_37_59.txt' ];
fid = fopen(filename,'r+');
flag = 1;
count = 1;
while ( flag )
temp = fgetl(fid);
flag = 0;
if ischar(temp)
flag = 1;
index = find(temp == ' ');
data_9030_27(1,count) = datenum(temp(1:index(1)-1),'dd_mmm_yyyy_HH_MM_SS');
data_9030_27(2,count) = str2num(temp(index(2)+1:length(temp)));
count = count+1;
end
end
data_9030_27(:,any(isnan(data_9030_27)))=[];%清洗数据,去除NaN
plot(data_9030_27(1,:),data_9030_27(2,:),'.r');
grid on
datetick('x','HH:MMPM')
xlabel('时间','fontsize',15)
ylabel('接收电平 (dBm)','fontsize',15)
title('9月27日 9030A实测电平值','fontsize',20)
print(gcf,'-djpeg','-r600','9月27日 9030A实测电平值.jpg');
save data_9030_27 data_9030_27

  

3、时间 距离显示

(1)

clear all
% fileID = fopen('C:\Users\Administrator\Desktop\舟山_9\波导气象数据\2017- 9-29- 7- 5-16.txt','r');
% fid=fopen('scr_new_2.txt','wt'); %新建一个txt文件
phns = ['9-27-trace.txt']; %要读取的文档所在的路径
fpn = fopen (phns, 'rt'); %打开文档
count = 1;
fgetl(fpn);%读取第一行表头
while feof(fpn) ~= 1 &count<1356 %用于判断文件指针p在其所指的文件中的位置,如果到文件末,函数返回1,否则返回0
temp = fgetl(fpn);
index_1 = find(temp == ',');
index_2=find(temp=='#');
index_3=find(temp==' ');
% date(count,1)=str2num(temp(1:index(1)-1));
% date(count,2)=str2double(temp(index(3)+1:index(4)-1));
% date(count,3)=str2double(temp(index(5)+1:index(6)-1));
% date(count,4)=str2double(temp(index(7)+1:index(8)-1));
% date(count,5)=str2double(temp(index(9)+1:index(10)-1));
% date(count,6)=str2double(temp(index(11)+1:index(12)-1));%年月日时分秒向量表示
% data_27(count,1) = datestr(date(count,:),31);%时间
% gps_27(count,1)=datenum(temp(1:index_3(4)-1));
gps_27(count,1)=datenum(temp(1:18));%时间转为数字
gps_27(count,2) = str2num(temp(index_1(1)+1:index_1(2)-1));%经度
gps_27(count,3) = str2double(temp(index_1(2)+1:index_2(1)-1));%纬度
% gps_27(count,2) = str2double(temp(index_1(28)+1:end));%波导高度
count = count+1;
end
gps_27(find(gps_27(:,2)==0),:)=[];
save gps_27 gps_27
% plot(gps_27(2:end,1)',gps_27(2:end,2)','->')

注意直接将时间字符串转为数字形式存储,时间格式如下

(2)坐标到距离

%%
% 计算时间-距离数据
clc
clear
tic
% 岸上平台位置
lat1 = 29.885063;
lon1 = 122.413249;
%
load gps_27.mat
num = size(gps_27,1);
for ii = 1:num
time_dis_27(ii,1) = gps_27(ii,1);%时间
lat2 = gps_27(ii,3);%纬度
lon2 = gps_27(ii,2);%经度
[arclen,az] = distance(lat1,lon1,lat2,lon2,referenceEllipsoid('wgs84'));
time_dis_27(ii,2) = arclen;%距离
end
plot(time_dis_27(:,1),time_dis_27(:,2)/1000,'b','linewidth',2)
set(gca,'fontsize',12)
x = time_dis_27(:,1);
datetick('x','HH:MM')
xlabel('Time (9/27/2017)')
ylabel('Distance (km)')
title('9月27日航线 时间-距离 图')
grid on
print(gcf,'-r600','-djpeg','9月27日航线时间-距离图.jpg')
% save time_dis_27.mat time_dis_27
toc

4、跳过错误

(1)

clear all
for i=1:5
a={[1;1];[1];[1;1];[1];[1]};
b=1;
try
c(i)=a{i,1}(2,1)+b;
end
end

i=2,4,5时出错

出错正常保证循环

(2)

a=1;
b=1;
try
c=a(1,2)+b %出错
catch
c=a(1,1)+b %运行这个程序
end
d=a+b

(3)

 clear all
for i=1:5
a={[1;1];[1];[1;1];[1];[1]};
b=1;
try
c=a{i,1}(2,1)+b;
catch
i
end
end

爆出出错位置。

5、时间交错

%%
clear
close all
clc
%%
%距离和电平值联系起来
load data_9030_27.mat %时间,电平值
load time_dis_27.mat%时间,距离
%%
%电平值找位置
num_1 = size(data_9030_27,2);%电平值个数14512
n=0;
for index = 1:num_1
try %错误了继续
temp = max(find(abs(data_9030_27(1,index)-time_dis_27(:,1))<(2/3600/24) ));%max 是最接近的数
path_loss_9030_27(index,1) = data_9030_27(1,index);%时间
path_loss_9030_27(index,2) = time_dis_27(temp,2);%距离获取
path_loss_9030_27(index,3) = data_9030_27(2,index);%电平值
catch
n=n+1;
y(n)=index;%显示异常不匹配的时间下标
end
end
path_loss_9030_27(find(path_loss_9030_27(:,2)==0),:)=[];%清洗不匹配的值
rsl_cexian1_3308B = path_loss_9030_27(:,3);
pt_cexian1_3308B = 20;
gt_cexian1_3308B = 20;
gr_cexian1_3308B = 30;
il_cexian1_3308B = 21+6;
pl_cexian1_3308B=pt_cexian1_3308B+gt_cexian1_3308B+gr_cexian1_3308B-il_cexian1_3308B-rsl_cexian1_3308B;%路径损失
plot(path_loss_9030_27(:,2)/1000,pl_cexian1_3308B,'.b')
set(gca,'fontsize',12)
axis([0 60 100 250])
xlabel('Distance (km)','fontsize',14)
ylabel('Path Loss (dB)','fontsize',14)
set(gca,'ytick',100:25:250)
set(gca,'xtick',10:10:60)
set(gca,'ydir','reverse')%翻转不影响值,
grid on title('9月27日距离-路径损失图','fontsize',14);
print(gcf,'-djpeg','-r600','9月27日距离-路径损失图.jpg');
save path_loss_9030_27 path_loss_9030_27

电平值有14512个,距离值有1350。从上面图中可以看出电平值和距离值只有11点半到两点是重合的。折旧要求以时间为纽带,插值进来。最终结果可以看到只有1196个有效值。

 

MATLAB 文件读取(3)的更多相关文章

  1. matlab中读取txt数据文件(txt文本文档)

    matlab中读取txt数据文件(txt文本文档) 根据txt文档不同种类介绍不同的读取数据方法 一.纯数据文件(没有字母和中文,纯数字) 对于这种txt文档,从matalb中读取就简单多了 例如te ...

  2. matlab批量读取一个文件夹里类似命名的mat文件

    参考网址: Matlab读取同一路径下多个txt或mat文件总结 matlab 批量读取数据文件.mat .dat 整理:matlab批量读入数据文件的方法 首先命名方式体现在只是名字里数字有变化,其 ...

  3. Matlab批量读取文件夹文件

    现在有一个文件夹 里面有50个左右的txt文件 每个文件大概三万行 两列 第一列是字符串 第二列是浮点数字 我只需要读第二列 现在我想写一个.M文件 批量读取这个文件夹里的txt文件 读取完以后的数组 ...

  4. Matlab 从入门到精通 Chapter11 文件读取I/O

    11.1 工作空间数据读取 将工作空间的变量保存为文件,可以使用save命令.  save('filename') 将文件保存在当前目录下,文件名为filename.mat save('filenam ...

  5. MATLAB中TXT数据文件读取并写入元胞数组的方法与步骤

    一. TXT数据文件读取 Data = load('train.txt');   %简单的文件读取,这时在工作区可以看到导入的大数据变量Data 二.大数据变量Data装入元胞数组中 D = cell ...

  6. matlab逐行读取text文件,编写函数提取需要的文字

    在数学建模中遇到的数据比较难处理,而且给的是text格式,自己想了好长时间才编出来,现在分享一下,可以交流学习 目标的text文件是 只提取里面的数据 需要自编函数 clc,clear path='D ...

  7. matlab文件操作及读txt文件(fopen,fseek,fread,fclose)

    文件操作是一种重要的输入输出方式,即从数据文件读取数据或将结果写入数据文件.MATLAB提供了一系列低层输入输出函数,专门用于文件操作. 1.文件的打开与关闭 1)打开文件 在读写文件之前,必须先用f ...

  8. Matlab文件操作

    1.  Matlab文件操作主要有三个步骤:首先打开文件,然后对文件进行读写操作,最后要关闭文件. 2.  fid=fopen(文件名,打开方式) 'r' 只读,文件必须存在(缺省的打开方式) 'w' ...

  9. MATLAB文件操作及读txt文件

    转自:http://blog.csdn.net/vblittleboy/article/details/8049748 文件操作是一种重要的输入输出方式,即从数据文件读取数据或将结果写入数据文件.MA ...

随机推荐

  1. 峰哥说技术:04-Spring Boot基本配置

    Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 04 Spring Boot基本配置 1)容器的相关配置 在Spring Boot中可以内置Tomcat. ...

  2. Linux——如何将Red Hat Enterprise Linux 6的语言改为中文?

    第一步,打开终端,输入su -,获取超级用户权限,输入密码. 第二步,输入cd /etc/sysconfig,进入设置目录. 第三步,输入vi i18n,进入到配置文件. 第四步,按 ‘i’键,进入编 ...

  3. 对两个有序数组重新去重合并排序js实现

    这里主要是要利用两个数组有序这个条件,所以只需两个指针分别指向两个数组,当其中一个小于另外一个就移动该指针,反之则移动另外一个指针,如果相等则均向后移动. 结束条件是,当任意一个数组的指针移到末尾则跳 ...

  4. 计算属性(computed)+侦听器(watch)+ 方法(methods)

    计算属性 computed 当数据改变时,方法的结果也会发生改变.如果多处地方调用计算属性里面的同一个方法时,该方法只会执行一次.如图,在控制台改变data里面的num值时,虽然在多处使用comput ...

  5. Linux查看目录树形结构

    安装tree. yum -y install tree 查看是否安装成功 yum list installed tree 执行tree命令查看目录树形结构 tree

  6. 附014.Kubernetes Prometheus+Grafana+EFK+Kibana+Glusterfs整合解决方案

    一 glusterfs存储集群部署 注意:以下为简略步骤,详情参考<附009.Kubernetes永久存储之GlusterFS独立部署>. 1.1 架构示意 略 1.2 相关规划 主机 I ...

  7. tableZen maxHeight 解决方案 如果数据条数小于N,不进行高度设置,超过N条,直接设置高度,解决原生iview Table 对于右侧固定列,不能计算出正确数值的解决方案

    tableZen maxHeight 解决方案 如果数据条数小于N,不进行高度设置,超过N条,直接设置高度,解决原生iview Table 对于右侧固定列,不能计算出正确数值的解决方案 if (thi ...

  8. 还记得第一个看到的Flutter组件吗?

    注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 MaterialApp 在学习Flutter的过程中我们第 ...

  9. 【Weiss】【第03章】练习3.4、3.5:有序链表求交、并

    [练习3.4] 给定两个已排序的表L1和L2,只使用基本的表操作编写计算L1∩L2的过程. [练习3.5] 给定两个已排序的表L1和L2,只使用基本的表操作编写计算L1∪L2的过程. 思路比较简单,测 ...

  10. Python基础 | 数据文件的读写

    目录 txt txt的读入 txt的写出 csv xls\xlsx 在线网页数据 常用的工具 爬虫的步骤 pdf pdfrw PyPDF2 提取文档信息 word文档 其他统计软件生成文件 本文总结使 ...