How to identify text files with zero value
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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
채택된 답변
"...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
Sir,
Thank you for suggestion.
I want to separate the the text files with zero vales in another folder. I don't know how to do that.
so in the code how to separate the text files with zero values in another folder.
if any(data)
%...do what need to do for those with some nonzero values here
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('*.txt'); % substitute suitable wildcard for your case to identify wanted files
for i=1:numel(d)
thisfile = fullfile(d(i).folder,d(i).name);
data = readmatrix(thisfile);
if any(data)
movefile(thisfile, folder_to_move_rest);
else
movefile(thisfile, folder_to_move_allzero);
end
end
Tanmoyee Bhattacharya
2023년 4월 19일
편집: Tanmoyee Bhattacharya
2023년 4월 19일
Sir, In this code the message is coming Reference to non-existent field 'folder'. I am using matlab version 2013b. How to resolve the problem?
That's a pretty old release by now, but dir() predates it so that isn't the problem. But, without the full error message in context with the exact code you ran, we can't really say anything except that it, alone, would be ok(*). That, of course, implies that your code got far enough along to have created the dir() structure and the reference isn't to some other pre-existing variable that isn't the output of a call to the function. We simply can't see/tell without context...
(*) Of course, your directory search may not have returned any results, but dir() will return an empty struct in that case; the struct names will still exist; there just won't be any content. Hence, that isn't the cause of the error message, either.
But, when you resolve that, then readmatrix will fail because it wasn't introduced until R2019a; you'll have to revert to one of the earlier forms to input the data file; see textread for a way that approximates what readmatrix would have done that also takes the file name rather than needing to open/close a file handle.
data=textread(d(i).folder,d(i).name,'%f');
would work if the data in the files does consist of just the one column; now you'll have to be much more aware of the actual file structure; most of the more user-friendly higher-level functions that attempt to remove the need to delve into the file structure itself postdate your release.
Tanmoyee Bhattacharya
2023년 4월 19일
편집: Walter Roberson
2023년 4월 19일
Sir, Thankyou for your guidance. I have solved it.
If I have a folder named file has text files with all zero and all zero and non-zero values I can separate the files in two different folders by the code
clc;
clear all;
folder='C:\Users\DELL\Desktop\file';
S=dir(fullfile(folder,'\*.txt'));
% temp = importdata(fullfile(folder,S(1).name));
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
for ii=1:numel(S)
thisfile=fullfile(folder,S(ii).name);
data = textread(fullfile(folder,S(ii).name));
% data=readmatrix(thisfile);
if any(data)
movefile(thisfile, folder_to_move_rest);
else
movefile(thisfile, folder_to_move_allzero);
end
end
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
2023년 4월 19일
I certainly had forgotten that fact, Walter...
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
참고 항목
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)
