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. 在centos7使用docker下搭建elasticsearch集群

    一 .docker的安装 https://www.cnblogs.com/ghostdot/p/12410242.html 二.创建相关映射文件 cd /home/ mkdir node cd nod ...

  2. tfgan折腾笔记(三):核心函数详述——gan_loss族

    gan_loss族的函数有: 1.gan_loss: 函数原型: def gan_loss( # GANModel. model, # Loss functions. generator_loss_f ...

  3. SPI总线传输的4种模式

    概述 在芯片的资料上,有两个非常特殊的寄存器配置位,分别是 CPOL (Clock POlarity)和 CPHA (Clock PHAse). CPOL配置SPI总线的极性 CPHA配置SPI总线的 ...

  4. django验证码框架captcha

    1.安装 2.在settings.py 安装app中添加 3.添加url 4.运行makemigrations和migrate 5.运用 在form表单中定义 view中返回form表单 在前端htm ...

  5. Linux下git使用

    一.安装 本人使用的是centos 7,首先安装git 1.下载git:wget https://Github.com/Git/Git/archive/v2.3.0.tar.gz 2.下载之后解压:t ...

  6. nes 红白机模拟器 第4篇 linux 手柄驱动支持

    小霸王学习机的真实手柄,实测CPU 占用 80% 接线图: 手柄读时序: joypad.c 驱动: 普通的字符设备驱动. #include <linux/module.h> #includ ...

  7. python学习的新篇章--面向对象

    面向对象的学习笔记   关键要素: 类:class 用来描述具有相同的属性和方法的对象的集合,它定义了该集合中每个对象所共有的属性和方法.   数据成员: 类的不同属性数据   对象: 类的一个实例 ...

  8. mysql实现读写分离

    MySQL读写分离概述 1.读写分离介绍 对于目前单机运行MySQL服务.会导致MySQL连接数过多.最终导致mysql的宕机.因此可以使用多台MySQL服务器一起承担压力.考虑到项目中读写比例的不一 ...

  9. git提交更改都是一个作者

    为什么提交到github的commit都是一个作者 参考链接 重要知识点讲解 问题如下所示 git是分布式去中心化的管理系统 ssh秘钥对生成.并把id_rsa.pub加入github.com中(这个 ...

  10. Journal of Proteome Research | SAAVpedia: identification, functional annotation, and retrieval of single amino acid variants for proteogenomic interpretation | SAAV的识别、功能注释和检索 | (解读人:徐洪凯)

    文献名:SAAVpedia: identification, functional annotation, and retrieval of single amino acid variants fo ...