zoukankan      html  css  js  c++  java
  • MATLAB 文件读取(3)

    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
    ',curr);
        end
    end
        
    fclose(fid_new);
    

    (3)

    clear all
    % fileID = fopen('C:UsersAdministratorDesktop2017- 9-29- 7- 5-16.txt','r');
    fid=fopen('scr_new_2.txt','wt');      %新建一个txt文件  
     phns = ['C:UsersAdministratorDesktop舟山_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
    ',new_str);%新的字符串写入当新建的txt文档中  
     end  
       fclose(fid);  
    

    按行读取

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

    clear all
    % fileID = fopen('C:UsersAdministratorDesktop舟山_9波导气象数据2017- 9-29- 7- 5-16.txt','r');
    fid=fopen('scr_new_2.txt','wt');      %新建一个txt文件  
    phns = ['C:UsersAdministratorDesktop舟山_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 
    ',wea_27{1,:});
     end  
    fclose(fid);
    save wea_27 wea_27

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

    (5)提取数据

    clear all
    % fileID = fopen('C:UsersAdministratorDesktop舟山_9波导气象数据2017- 9-29- 7- 5-16.txt','r');
    fid=fopen('scr_new_2.txt','wt');      %新建一个txt文件  
    phns = ['C:UsersAdministratorDesktop舟山_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:UsersAdministratorDesktop舟山_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个有效值。

     

  • 相关阅读:
    2020软件工程作业01
    问题清单
    2020软件工程个人作业06——软件工程实践总结作业
    感谢随笔
    2020软件工程作业05
    2020软件工程作业04
    2020软件工程作业03
    2020软件工程作业02
    2020软件工程作业01
    2020软件工程个人作业06——软件工程实践总结作业
  • 原文地址:https://www.cnblogs.com/ruo-li-suo-yi/p/8001107.html
Copyright © 2011-2022 走看看