I have a timetable that I'm converting to an array. Some of the columns are NaNs so initially, I just changed the columns to zeros before converting it but I don't want to do this since not all values in the column are necessarily NaNs and I don't want to lose that possible data. I was thinking of creating a loop so that if the values are NaNs, it'll be changed to a zero and if the value is something else, it'll be left alone. This is what I tried writing for a column called G
for c = 1:length(TT(:,'G'))
if TT(c,'G')= nan
TT(c,'G') = 0;
else
TT(c,'G') = TT(c,'G');
end
I know i'm calling the if function wrong but i'm not sure how to fix it. if statements always trip me up for some reason

 채택된 답변

Star Strider
Star Strider 2018년 10월 31일

0 개 추천

It might be easier to use the timetable2table (link) function, then the table2array (link) function.
I have no idea if this would work with your data. You will have to experiment.

댓글 수: 6

Susan Santiago
Susan Santiago 2018년 10월 31일
Thanks for the advice but that doesn't work in this case. All of the values need to be the same variable type
Star Strider
Star Strider 2018년 10월 31일
My pleasure.
We would have to see at least a representative sample of your timetable, ideally as a ‘mat’ file, so all the variable types and other relevant information is available.
Susan Santiago
Susan Santiago 2018년 10월 31일
This is the time table. As you can see, there are 4 columns that are all NaNs but that won't necessarily always be the case
It was something of a challenge for me to figure out how to convert your 'NAN' string entries to numeric NaN values. I am still not certain what you want to do.
This will at least get you started (assuming you already have your ‘TT’ table in your workspace):
TT_T = timetable2table(TT); % Convert To Table
TT_C = table2cell(TT_T); % Convert To Cell
TT_L = cellfun(@(x)strcmpi(x, 'NAN'), TT_C); % Find 'NAN' Strings
TT_C(TT_L) = {NaN}; % Convert String 'NAN' To Numeric {NaN}
TT_M = cell2mat(TT_C(:, 3:end)); % Data Without ‘datetime’ Objects
The final version of ‘TT_C’ is a cell array with the string 'NAN' elements replaced with numeric NaN elements. The ‘TT_M’ matrix is an array of the data without the first two columns. If you want to include them, use the datenum function to convert them to numeric values. I would not recommend that, although it could be necessary, depending on how you choose to process your data.
Susan Santiago
Susan Santiago 2018년 10월 31일
This is perfect, thanks so much!
Star Strider
Star Strider 2018년 10월 31일
As always, my pleasure!

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2018년 10월 31일
편집: Andrei Bobrov 2018년 10월 31일

0 개 추천

zrs = zeros(size(TT,1),1);
TT.G = zrs;
TT.SG = zrs;

댓글 수: 3

Susan Santiago
Susan Santiago 2018년 10월 31일
Can you explain this a little bit. Would this replace the code I posted or be part of it. I tried running this in my code and it said "Undefined function 'isnan' for input arguments of type 'timetable'.
Error in flux_loop2 (line 28) out(isnan(out)) = 0;"
Andrei Bobrov
Andrei Bobrov 2018년 10월 31일
I am fixed my answer.
Susan Santiago
Susan Santiago 2018년 10월 31일
This would just change the whole column to zeros wouldn't it? I explained that that's not what I want to do

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

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

질문:

2018년 10월 31일

댓글:

2018년 10월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by