필터 지우기
필터 지우기

Why do i get NaN?

조회 수: 1 (최근 30일)
Tim K
Tim K 2021년 5월 6일
댓글: Tim K 2021년 5월 6일
Dear all,
i try to run this very simpel Code (it worked already with other Input files with identical build-up). I try to import a table with 2 columns from 6 .csv files each and want to declare a starting point in column 1 for every table with "anfang" to average all values from column 2 from the starting point to end. As i said it works with other .csv files but in this case i only get an average value for the first 2 files. For the other 4 files i get "NaN". I tried to debug the code but i dont get any errors at this point and i also checked the columns of the imported tables but everything seems good (no critical values that could affect mathematical operations or something else). I also tried to change the values in "anfang" to try a earlier or later starting point but its also didn't work.
Thanks for help
clear all
close all
%%
filename = dir('*.csv');
%% avgT
anfang = [700,700,200,100,140,140];
%%
for idx1 = 1:length(filename)
Cell{idx1} = readtable(filename(idx1).name);
Cell{idx1} = table2array(Cell{idx1});
avgT (idx1) = mean(Cell{idx1}(anfang:end,2))
end
  댓글 수: 2
KSSV
KSSV 2021년 5월 6일
Try using nanmean instead of mean.
Tim K
Tim K 2021년 5월 6일
I could solve this problem with some help in an other way but thank you for your time!

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

답변 (1개)

Scott MacKenzie
Scott MacKenzie 2021년 5월 6일
편집: Scott MacKenzie 2021년 5월 6일
It's hard to say without being able to run your code, but it seems to me that anfang in
avgT(idx1) = mean(Cell{idx1}(anfang:end,2));
should include an index. Try
avgT(idx1) = mean(Cell{idx1}(anfang(idx1):end,2));
Also, why are you putting the data from the files into a cell array of cell arrays? The lines in your for-loop can be simplified. Try
T = readtable(filename(idx1).name);
C = table2array(T);
avgT(idx1) = mean(C(anfang(idx1):end,2));
  댓글 수: 1
Tim K
Tim K 2021년 5월 6일
Thank you very much! Inlcude the index at "anfang" worked well. And also thank you for simplifications!

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by