2014-06-05 2 views
0

텍스트 파일에서 한 줄을 읽는 MATLAB 스크립트가 있습니다. 텍스트 파일의 각 행에는 CSV의 파일 이름이 들어 있습니다. MATLAB이 작업하고있는 라인을 추적하여 셀 배열에 해당 라인의 데이터를 저장할 수 있어야합니다. 어떻게해야합니까? 설명하기MATLAB이 작업중인 .dat 파일의 행을 찾으십시오.

내 .DAT 파일의 처음 몇 줄은 다음과 같습니다

2006-01-003-0010.mat 
2006-01-027-0001.mat 
2006-01-033-1002.mat 
2006-01-051-0001.mat 
2006-01-055-0011.mat 
2006-01-069-0004.mat 
2006-01-073-0023.mat 
2006-01-073-1003.mat 
2006-01-073-1005.mat 
2006-01-073-1009.mat 
2006-01-073-1010.mat 
2006-01-073-2006.mat 
2006-01-073-5002.mat 
2006-01-073-5003.mat 

내가 O3_data의 다른 셀에이 .mat 각 파일에서 변수 site_data을 저장해야합니다. 따라서 O3_data{1}은 텍스트 파일의 첫 번째 줄에있는 데이터이고, O3_data{2}은 두 번째 줄의 데이터입니다.

이 코드는 작동하지만 카운터를 사용하지 않아도됩니다. 내가 MATLAB 내가 난 내용이 무엇인지 알 수 없도록 Subscript indices must either be real positive integers or logicals.nt 정수라고 말해 된 time index 부분을 추가하는 경우

year = 2006:2014; 
for y = 1:9 
    flist = fopen(['MDA8_' num2str(year(y)) '_mat.dat']); % Open the list of file names - CSV files of states with data under consideration 
    nt = 0; % Counter will go up one for each file loaded 

    while ~feof(flist) % While end of file has not been reached 
     fname = fgetl(flist); 
     disp(fname); % Stores name as string in fname 
     fid = fopen(fname); 

     while ~feof(fid) 
      currentLine = fgetl(fid);  
      load (fname, 'site_data'); % Load current file. It is all the data for one site for one year 
      O3_data = site_data; 
      % Do other stuff 
     end 
     fclose(fid); 
    end 
    fclose(flist); 
end 

: 나는 단지 내가에서 읽고 있어요 파일 중 하나에 대한 데이터를 얻을 수 잘못하고있다. 내가 필요로하는 time index 나는 O3_data {난}하는 각 i 내가에서 읽고 있어요 파일 중 하나입니다

year = 2006:2014; 
for y = 1:9 
    flist = fopen(['MDA8_O3_' num2str(year(y)) '_mat.dat']); % Open the list of file names - CSV files of states with data under consideration 
    nt = 0; 

    while ~feof(flist) % While end of file has not been reached 
     fname = fgetl(flist); 
     fid = fopen(fname); 

     while ~feof(fid) 
      currentLine = fgetl(fid); 
      nt = nt+1; % Time index 
      load (fname, 'site_data'); % Load current file. It is all the data for one site for one year 
      O3_data{nt} = site_data; 
      % Do other stuff 
     end 
     fclose(fid); 
    end 
    fclose(flist); 
end 
+0

{시도 끝에 + 1} = site_data'는 초기 '으로 정의 된 곳 O3_data = {}'그런데 – Amro

+0

, 두 번째 while 루프 'fname'은 바뀌지 않으므로 동일한 데이터를 반복해서로드합니다 .. – Amro

+0

나는 믿을 수 없습니다. .dat 파일에는 1000 가지가 넘는 CSV 파일의 파일 이름이 들어 있습니다. 내가 때마다 다른 CSV 파일에서'site_data'에서로드 오전 믿습니다. – shizishan

답변

0

시도 다음`O3_data 사용하여`nt` 카운터 교체

years = 2006:2014; 
for y=1:numel(years) 
    % read list of filenames for this year (as a cell array of strings) 
    fid = fopen(sprintf('MDA8_O3_%d_mat.dat',years(y)), 'rt'); 
    fnames = textscan(fid, '%s'); 
    fnames = fnames{1}; 
    fclose(fid); 

    % load data from each MAT-file 
    O3_data = cell(numel(fnames),1); 
    for i=1:numel(fnames) 
     S = load(fnames{i}, 'site_data'); 
     O3_data{i} = S.site_data; 
    end 

    % do something with O3_data cell array ... 
end 
+0

이 오류가 발생합니다 :'Error using textscan 유효하지 않은 파일 식별자. fopen을 사용하여 유효한 파일 식별자를 생성하십시오.''fid'는''fopen ''을 수행 한 후 분명히'-1'입니다. – shizishan

+0

@shizishan : 죄송합니다. 두 번째 줄에 오타가 있습니다. 이제 해결되었습니다. 편집 : 알았어 두 가지 오타가 있었다 : – Amro

+0

'fnames = fnames {1};'행의 목적은 무엇인가? – shizishan

0

을 가질 수 있도록 다음과 같은 시도 -. 외부 for 루프가 있기 때문에 그주의의를 nt 변수는 해당 루프 외부에서 초기화해야 이전 연도 (또는 이전 j)의 데이터를 덮어 쓰지 않습니다. 방금 읽은 파일은 * .mat 파일이므로 while 루프를 피할 수 있으며 load 명령을 사용하여 단일 변수를 작업 영역으로로드합니다.

year = 2006:2014; 
nt = 0; 

data_03 = {}; % EDIT added this line to initialize to empty cell array 
       % note also the renaming from 03_data to data_03 

for y = 1:9 
    % Open the list of file names - CSV files of states with data under 
    % consideration 
    flist = fopen(['MDA8_O3_' num2str(year(y)) '_mat.dat']); 

    % make sure that the file identifier is valid 
    if flist>0 

     % While end of file has not been reached 
     while ~feof(flist) 
      % get the name of the *.mat file 
      fname = fgetl(flist); 

      % load the data into a temp structure 
      data = load(fname,'site_data'); 

      % save the data to the cell array 
      nt = nt + 1; 
      data_03{nt} = data.site_data; 
     end 

     fclose(flist); % EDIT moved this in to the if statement 
    end 

end 

위의 예는 위의 예에서 설명한대로 * .dat 파일에 * .mat 파일 목록이 있다고 가정합니다.

이전 게시물의 위 코드에있는 EDIT에주의하십시오.

+0

'03_data {nt} = data.site_data;'줄에서'The input 문자가 MATLAB 문이나 표현식에서 유효하지 않습니다. 왜 그런지 알고 있습니까? 나는 그것을 이해할 수 없다. – shizishan

+0

Yikes - 이걸 테스트 해봐야 겠어! MATLAB 변수는 '03_data'에서와 같이 숫자로 시작할 수 없습니다. 이것은'data_03' 또는 비슷한 것으로 변경되어야합니다. 이를 반영하기 위해 위 코드를 편집 할 것입니다. – Geoff

+0

아, 실제로는 0으로 시작하지 않습니다. 그게 편지인데. 고쳐 주셔서 고마워.그 이유를 알고있는 것이 좋습니다. – shizishan

관련 문제