2012-02-16 2 views
1

.txt 파일을 MATLAB으로 가져 오는 다음 함수를 작성했습니다. .txt 파일은 시간 간격 또는 4 분 간격으로 기록 할 수 있습니다. 초기 해상도에 따라 스크립트는 시간별 또는 일별 평균을 계산합니다. 단순히 스크립트, 즉 기능을 사용하지 그냥 두 번째 줄에 경로 이름을 입력하여 실행하면텍스트 파일에서 데이터를 가져 와서 평균을 계산하는 MATLAB 함수

function [Daily, Hourly] = calc_avg(pathName) 
    TopFolder = pathName; 
    dirListing = dir(fullfile(TopFolder,'*.txt'));%#Lists the folders in the directory 
                %#specified by pathName. 
    for i = 1:length(dirListing); 
     fileToRead1{i} = (dirListing(i).name); 
     %#lists all of the .txt files in the TopFolder 
    end 
    cell_all = arrayfun(@(i1)importdata(fullfile(pathName,dirListing(i1).name)),... 
     (1:length(dirListing))','un',0); %#' 

    %# Apply function to each element of the array. 
    d = cat(2,cell_all{:}); 

    %# Concatenate arrays along each column (i.e. 2). 
    %# Find the length of the dataset, which will provide information on the 
    %# amount of averaging required. 
    if length(d) == 365,... 
      error('error: daily averages already calculated'); %#' 
    elseif length(d) == 8760; 
     daily = squeeze(mean(reshape(d,24,size(d,1)/24,[]))); 
    elseif length(d) == 131400; 
     hourly = squeeze(mean(reshape(d,15,size(d,1)/15,[]))); 
     daily = squeeze(mean(reshape(d,360,size(d,1)/360,[]))); 
    end 

    %# Find which averages have been calculated: 
    A = exist('hourly','var'); 

    %# If A == 1 means that hourly values had to be calculated therefore 
    %# the data if of high resolution (minutes). 
    if A == 1; 
     hourly = mat2cell(hourly,size(hourly,1),cellfun('size',cell_all,2)).'; %#' 
     daily = mat2cell(daily,size(daily,1),cellfun('size',cell_all,2)).'; %#' 
    elseif A == 0; 
     daily = mat2cell(daily,size(daily,1),cellfun('size',cell_all,2)).';%#' 
    end 

    %# Create cell in the same format as 'cell_all' where cellfun applies the 
    %# same function to each cell in a cell array. 'size' is used to create 
    %# the same format. 
    for i=1:length(dirListing); 
     [~,name{i}] = fileparts(fileToRead1{i}); 
     %# Obtain the name of each of the .txt files (dirListing) 
    end 

    %#Generate a structure for the averages calculated. 
    if A == 1; 
     for i=1:length(dirListing); 
      Daily.(genvarname(name{i})) = daily{i}; 
      Hourly.(genvarname(name{i})) = hourly{i}; 
     end 
    elseif A == 0; 
     for i=1:length(dirListing); 
      Daily.(genvarname(name{i})) = daily{i}; 
     end 
end 

스크립트는 잘 작동합니다. 하지만 일단 기능으로 작동하지 않으면 노력하고 사용합니다. 오류가 발생합니다 :

Error in calc_avg (line 15) 
TopFolder = pathName; 

내가 뭘 잘못하고 있니? pathName이 문자열이기 때문에 문제가 발생합니까?

+0

calc_avg.m의 처음 13 줄에 대한 의견이 있다고 가정합니다. –

+0

오류 설명이 없습니까? – yuk

+0

예 첫 번째 줄은 주석입니다. A == 0이면 Hourly가 존재하지 않으므로 matlab에 오류가 표시되어 오류가 발생했습니다. 내가해야 할 일은 Hourly = []; 최종 루프에서. 지금해야 할 일은 생성 된 변수를 같은 디렉토리에 저장하는 방법을 찾는 것입니다. 당신의 도움을 주셔서 감사합니다. – Emma

답변

4

있습니다 (MATLAB 경로에 현재 폴더 또는 폴더에) calc_avg.m라는 별도의 파일에서 함수를 저장하고

pathName = 'path/to/file'; 
[Daily, Hourly] = calc_avg(pathName) 

같은 별도의 스크립트 또는 명령 줄에서 실행해야 당신은 아마를 얻을 수 Run (f5)을 사용하여 편집기 내에서 스크립트로 함수를 실행하려고하기 때문에 오류가 발생했습니다.

관련 문제