現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
I have made a filter as following
[b,a] = butter(9,.02,'low'); x=textread('noisy.out'); y=filter(b,a,x); save filtered.out y -ASCII
I have 1000 files and I would like to filter all my files using the above filter. Is there any way that I can do this efficiently? my files are named like 1.out 2.out , ... 1000.out
any suggestion would be appreciated.
my *.out files just include one column with numbers
this is one example -4.034E+02 -4.007E+02 -3.987E+02 -3.978E+02
All numbers in one column and each file contains 13000 numbers
採用された回答
Oleg Komarov
2011 年 3 月 11 日
Parse the folder for existing 1-1000.out files and import them one-by-one apply filter and save:
% Retrieve all the files in a directory
names = dir('C:\Users\Oleg\Desktop\Nuova cartella');
names = {names(~[names.isdir]).name};
% Select only the files #.out
idx = ~cellfun('isempty',regexp(names,'\d+\.out'));
names = names(idx);
% Filter
[b,a] = butter(9,.02,'low');
for n = names
% Open file for reading
fid = fopen([myDir n{:}],'r');
x = textscan(fid,'%f');
y = filter(b,a,cat(1,x{:}));
% Discard content and write filtered data
fid = fopen([myDir n{:}],'w');
fprintf(fid,'%E\r\n',y);
fid = fclose(fid);
end
Oleg
12 件のコメント
maadi
2011 年 3 月 11 日
I do not want to import them one-by-one. I wanna get this done by single run. I got 1000 files and I have to repeat this for many times.
Walter Roberson
2011 年 3 月 11 日
There is no Mathworks provided way to import multiple files in a single mathworks provided routine. You can write your own routine that does a loop importing them one by one, but the effect will be little different than Oleg's loop.
If you _do_ read all of the files at one time, and if they are all exactly the same number of data points, then you can store the data as columns in an array, and then you can filter(b,a,TheArray) to do each of the columns. Or store the data as rows and specify the dim of 2 in the filter() call (columns is more efficient though.)
Your question was not clear as to the file names you want to store the filtered results in to, and not clear whether you wanted to store one output file per input file or just one large output file containing all of the filtered data.
maadi
2011 年 3 月 12 日
Let me make it clear
each signal is put in a text file and I have named them 1.out 2.out . . . 1000.out
I want to filter my signals and removes noise and after that I name my files again 1.out 2.out .... 1000.out
I just wanna get rid of noise in my signals.
I can not put all my files in single file because every file contains 13000 data and if I put them all in one file it becomes very huge and my computer will not be able to process that
Thanks
Walter Roberson
2011 年 3 月 12 日
The loop that Oleg shows will read in each of the files in turn; you do not need to repeat it or manually run it for each file.
Oleg Komarov
2011 年 3 月 12 日
@maadi: it's standard procedure the one outlined above as Walter confirms. Each loop just imports one file, filters it and save the filtered version to a file (may also overwrite it). This operation is repeated for each in your folder that begins with a number and has .out extension.
There's no other way to do it with an essentially different method.
1.Import data
2.Work on data
3.Export data
Repeat 1,2,3 till no more data is available.
maadi
2011 年 3 月 12 日
Oleg,
I put all my *.out files in a folder.
When I run your program I get this error
??? Function 'exist' is not defined for values of class 'cell'.
Error in ==> exist at 41
[varargout{1:nargout}] = builtin('exist', varargin{:});
Error in ==> textread at 160
if (exist(varargin{1}) ~= 2 | exist(fullfile(cd,varargin{1})) ~= 2) & ~isempty(which(varargin{1}))
Error in ==> Oleg at 12
x = textread(n);
why is that?
Oleg Komarov
2011 年 3 月 12 日
You have to specify the format:
textread(filename,format)
I edited the solution above. Format should be a string.
maadi
2011 年 3 月 12 日
I am very sorry I am new at MATLAB.
I run the following m file
clc
clear all
% Retrieve all the files in a directory
names = dir('d:\MATLAB7\work');
names = {names(~[names.isdir]).name};
% Select only the files #.out
idx = ~cellfun('isempty',regexp(names,'\d+\.out'));
names = names(idx);
% Filter
[b,a] = butter(9,.02,'low');
for n = names
x = textread(n,format);
y = filter(b,a,x);
% save...
end
But now I get this error
??? Error using ==> format
Too many output arguments.
Error in ==> Oleg at 12
x = textread(n,format);
My feeling is something is wrong with filename n
can you also change your program to overwrite every file after filtration?
Thanks
Oleg Komarov
2011 年 3 月 12 日
So, to get it right you have to specify the format as (for example): '%9c %6s %*f %2d %3s'.
I can help here, but I need to see, say, the first three lines of a sample #.out file (headers included). Maybe edit your main post adding these lines so that I can specify the format and add the saving part.
Walter Roberson
2011 年 3 月 12 日
And to clarify, the immediate reason why it said "Too many output arguments" is that "format" is a Matlab command that does not return any value.
(This is the sort of reason I use variables with uppercase when I mean the user to fill in values.)
Oleg Komarov
2011 年 3 月 12 日
Ok I modified the script accordingly. Be careful: the content is imported, filtered and overwritten. Make some test files.
maadi
2011 年 3 月 13 日
Thank you Oleg I really appreciate that
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Filter Analysis についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
