2013-06-12 24 views
0

您好我正在嘗試創建一個matlab腳本,它讀取目錄中的所有文件,併爲每個文件擴展名啓動不同的命令。 我:基於文件擴展名啓動Matlab腳本

teqc1.azi 
teqc1.ele 
teqc1.sn1 
teqc2.azi 
teqc**** 

我需要的是該腳本讀取文件和遞歸啓動命令:

`teqc1.azi -> plot_compact_2(teqc1.azi)` 
`teqc1.ele -> plot_compact_2(teqc1.ele)` 
`teqc1.sn1 -> plot_compact_2(teqc1.sn1)` 
`teqc**** -> plot_compact_2(teqc****)` 

這是我想出到現在:

function plot_teqc 


d=dir('*'); % <- retrieve all names: file(s) and folder(s) 
    d=d(~[d.isdir]); % <- keep file name(s), only 
    d={d.name}.'; % <- file name(s) 
    nf=name(d); 
for i=1:nf 

    plot_compact_2(d,'gps'); 
% type(d{i}); 
end 

謝謝

+3

你有什麼迄今所做?你有什麼嘗試?如果你需要幫助,那麼你需要首先證明你已經付出了努力。如果你只需要有人根據標準爲你編寫代碼,那麼你可以聘請自由職業者...... – Sander

+0

你說得對,那很愚蠢。我已經添加了我的「努力」:) – RColombo

+0

首先,你說你想爲每個文件擴展名運行「不同的命令」,但是你總是運行同一個命令--' plot_compact_2'。你能說清楚嗎? –

回答

1

然後你需要dir功能列出文件夾內容和fileparts函數以獲取擴展名。

您還可以查看this question以查看如何獲取與某個掩碼匹配的目錄中的所有文件的列表。

所以:

% get folder contents 
filelist = dir('/path/to/directory'); 
% keep only files, subdirectories will be removed 
filelist = {filelist([filelist.isdir] == 0).name}; 

% loop through files 
for i=1:numel(filelist) 
    % call your function, give the full file path as parameter 
    plot_compact_2(fullfile('/path/to/directory', filelist{i})); 
end