이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
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에서 Transforms에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
