Delete rows with zero after importing several files

1 次查看(过去 30 天)
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 个)

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 中查找有关 Whos 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by