How to identify text files with zero value

조회 수: 1 (최근 30일)
Tanmoyee Bhattacharya
Tanmoyee Bhattacharya 2023년 4월 17일
댓글: dpb 2023년 4월 19일
I have 10 text files and each text file has 9 rows and one column. In some text file total column is zero. Like
0
0
0
0
In other file there are zero values and numbers. Like
0
1
2
0
3
4
5
I want to extract text files with total column of zero values
0
0
0
0
0
If there is any way to extract the text files

채택된 답변

dpb
dpb 2023년 4월 17일
이동: dpb 2023년 4월 17일
"...is any way to extract the text files[?]"
What do you mean by "extract", precisely? What is the end result you're after; to read the data files that actually contain one or more nonzero values, identify which files are those that contain only zero values, ...?
The answer to all of those will be based on simply reading each file in turn and testing the content something like
d=dir('*.txt'); % substitute suitable wildcard for your case to identify wanted files
for i=1:numel(d)
data=readmatrix(fullfile(d(i).folder,d(i).name));
if any(data)
%...do what need to do for those with some nonzero values here
else
%...and all zeros here...
end
end
  댓글 수: 7
Walter Roberson
Walter Roberson 2023년 4월 19일
편집: Walter Roberson 2023년 4월 19일
Old enough versions of MATLAB did not have the .folder property of dir() results.
%the below logic requires that the data file be just a list of numbers,
%one per line, with no headers. (Or if it has headers, the header line must
%start with '%' characters.)
%if this is not true, if there are header lines, the code would need to be
%modified
folder_to_search = 'C:\Users\DELL\Desktop\file';
folder_to_move_allzero = './all0';
folder_to_move_rest = './not0';
if ~isdir(folder_to_move_allzero); mkdir(folder_to_move_allzero); end
if ~isdir(folder_to_move_rest); mkdir(folder_to_move_rest); end
d = dir( fullfile(folder_to_search, '*.txt')); % substitute suitable wildcard for your case to identify wanted files
for i=1:numel(d)
thisfile = fullfile(folder_to_search,d(i).name);
data = load(thisfile, '-ascii');
if any(data)
movefile(thisfile, folder_to_move_rest);
else
movefile(thisfile, folder_to_move_allzero);
end
end
dpb
dpb 2023년 4월 19일
I certainly had forgotten that fact, Walter...

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by