read from and write to file

使用两种方法读数据

clc;clearvars;
fid=fopen('data.txt');
tline=fgetl(fid);
nline=1;%表示行数
while ischar(tline)
    %do some work
    %first method:use strfind(tline,' ');'seperator'
    %data type:12 30 8 23 9 40
    %          30 12 11 3 6 7
    %%-------------------------------
%     index=strfind(tline,' ');
%     newindex=[0 index length(tline)+1];%在前面和后面多增加一个seperator
%     for i=1:length(newindex)-1
%         str=tline(newindex(i)+1:newindex(i+1)-1);
%         data{nline,i}=str;
%     end
%     nline=nline+1;
%     tline=fgetl(fid);%获取下一行
    %%---------------------------------
    %second method:use textscan(tline,'%s'),以cell的形式扫入到字符串 
    str_c=textscan(tline,'%s');
    data{nline}=str_c;
    nline=nline+1;
    tline=fgetl(fid);
end

posted on 2016-09-13 09:14  Raby_Draby  阅读(155)  评论(0)    收藏  举报

导航