2012-02-16 15 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

あなたはおそらく取得

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

として別のファイルに機能を保存する必要があります(MATLABパスの現在のフォルダまたはフォルダ内)calc_avg.mと呼ばれ、別のスクリプトやコマンドラインから実行Run(f5)でエディタ内でスクリプトとして関数を実行しようとしているため、エラーが発生しました。

関連する問題