필터 지우기
필터 지우기

Delete rows with zero after importing several files

조회 수: 1 (최근 30일)
Tatjana Mü
Tatjana Mü 2022년 4월 28일
댓글: Tatjana Mü 2022년 4월 28일
Good Morning,
I have a problem with my code and sadly dont find a solution. I import several csv files.
directory_name=uigetdir('','Ordner mit Messungen auswählen');
[nur_file_name,pfad]=uigetfile({'*.csv','csv-files (*.csv)';'*.*','all Files'},...
'Die csv-Files der Proben oeffnen (probe_001.csv=',[directory_name '/'], 'Multiselect', 'on');
nur_file_name=cellstr(nur_file_name);
nur_file_name=sort(nur_file_name);
filename=strcat(pfad,nur_file_name);
anzahl_files=size(filename,2); %number of imported files
for xy=1:anzahl_files
fid_in=fopen(char(filename(xy)),'r');
Afterwards I do some calculation with every file, so that in the end I get a ratio double array for every file.
id = true(anzahl_runs,1) ;
for k=1:anzahl_runs
if any (intens_RL(k,:)==1)
Ratio(k,1)=SpalteSr87(k,:)/SpalteSr86(k,:);
Ratio(k,2)=[[[SpalteNd143(k,:)/SpalteNd144(k,:)]/0.512638]-1]*10^4;
Ratio(k,3)=SpalteNd143(k,:)/SpalteNd144(k,:);
Ratio(k,4)=SpalteOs187(k,:)/SpalteOs188(k,:);
id(k) = 0 ;
end
end
In the next step I change the NaN und Inf to zero. Which is working quite well. But then I want to delete the rows which just contain zeros. And here is the problem! If I read in two files one ratio double array is correct, but the next one is mixed with correct values and values from the file before. Maybe the problem is that the rows are not constant. But I am totally stuck in that problem.
Ratio(isinf(Ratio))=0; %Inf and NaN replaced by zero
Ratio(isnan(Ratio))=0;
Ratio(all(~Ratio,2),:)=[]; %all rows with just zero get deleted
end
I hope somebody of you can help me. I thank you really much in advance.
  댓글 수: 1
Riccardo Scorretti
Riccardo Scorretti 2022년 4월 28일
Could you provide some files so that we can make your code run?

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

답변 (1개)

KSSV
KSSV 2022년 4월 28일
If you want to remove rows which have zeros in specific columns do:
c = 2; % column to check for zero
idx = Ratio(:,c)==0 ;
Ratio(idx,:) = [] ;
If you want to check for all columns do:
idx = any(Ratio,2) ;
Ratio(~idx,:) = [] ;
  댓글 수: 1
Tatjana Mü
Tatjana Mü 2022년 4월 28일
It is not working. If I add it outside of the for/if loop, I dont even know what it is doing. Inside of the for/if loop, it causes the same problem like my initial code.

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

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by