Re: [問題] txt讀檔如何只讀某幾行

看板MATLAB作者 (:))時間14年前 (2011/11/22 12:54), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串5/5 (看更多)
先謝謝原文讓我有頭緒,想請教一下如何讓我的程式變快一點 因為現在的寫法感覺笨了點,跑起來很慢,還請各位指教 -- 假設我想要讀的檔案中有A、B、C、D 四個變數,變數間的size不一定一樣 檔案的排序如下,數字代表時間的不同 A1 (3*10的矩陣) B1 (2*10的矩陣) C1 (1*10的矩陣) D1 (7*10的矩陣) A2 (3*10的矩陣) B2 (2*10的矩陣) C2 (1*10的矩陣) D2 (7*10的矩陣) . . . . 程式的output我想要將每個變數分別存檔 以下是我程式的寫法,因為一個文字檔有214MB,而變數有17個 每次讀一個檔案都要好久,不知道有沒有比較好的辦法來加快讀取的速度 -- clear clc parameters=[' A';' B';' C';' D';' A']; L_p=length(parameters); new_reading=1; for index_par=1:L_p-1 fid=fopen('test.txt'); read_data=[]; i=0; while new_reading ~= -1 new_reading=fgets(fid); index_word=strfind(new_reading,parameters(index_par,1:4)); if index_word==1 i=i+1; new_reading=fgets(fid); [read_data]=[read_data,str2num(new_reading)]; new_reading=fgets(fid); index_word=strfind(new_reading,parameters(index_par+1,1:4)); while isempty(index_word)==1 & new_reading ~= -1 [read_data]=[read_data,str2num(new_reading)]; new_reading=fgets(fid); index_word=strfind(new_reading,parameters(index_par+1,1:4)); end output{i}=read_data; end read_data=[]; end % 開始存檔 if index_par==1 A=output; save A.mat A elseif index_par==2 B=output; save B.mat B elseif index_par==3 C=output; save C.mat C elseif index_par==4 D=output; save D.mat D end output=[]; new_reading=1; %initial input fclose(fid); end -- 如果表達得不好,還請見諒,先謝過各位了 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 136.186.19.184 ※ 編輯: MPL 來自: 136.186.19.184 (11/22 12:55)
文章代碼(AID): #1EooiHm6 (MATLAB)
文章代碼(AID): #1EooiHm6 (MATLAB)