I have a timetable that has 4 columns that are Date, NOConc, NO2Conc, NOxConc. I want to convert any negative numbers in the NOConc column to zero. The S function pulls the data from the larger timetable, TN_1, for a specific time range that I'm looking at (in this case 5/11/2022 - 5/18/2022) and makes the new timetable labeled Holly_NOx, with a variable amount of rows depending on the given date range. This code is the closest I've gotten, but it gives me the error "A variable index must be a real positive integer." I've seen some people say matlab wont allow indexing of negative integers, which would make what I'm trying to do impossible. Thanks!!
S = timerange('5/11/2022' , '5/18/2022', 'days');
Holly_NOx = TN_1(S,:);
if Holly_NOx.(Holly_NOx.NO2Conc) > 0
Holly_NOx.(Holly_NOx.NO2Conc) = 0;
end

 채택된 답변

Voss
Voss 2022년 6월 9일
편집: Voss 2022년 6월 9일
% a timetable with 3 data columns:
Holly_NOx = timetable(datetime(now+(-29:-22),'ConvertFrom','datenum').', ...
randn(8,1),randn(8,1),randn(8,1), ...
'VariableNames',{'NOConc' 'NO2Conc' 'NOxConc'});
disp(Holly_NOx);
Time NOConc NO2Conc NOxConc ____________________ ________ ________ _________ 11-May-2022 20:36:01 0.56796 1.792 0.34045 12-May-2022 20:36:01 0.71024 -0.44977 2 13-May-2022 20:36:01 -0.98089 -0.96963 -1.1307 14-May-2022 20:36:01 -1.2461 1.1944 -0.14942 15-May-2022 20:36:01 0.32287 -0.67921 0.20059 16-May-2022 20:36:01 -0.98689 2.5576 1.0473 17-May-2022 20:36:01 -1.219 1.117 1.023 18-May-2022 20:36:01 0.82632 -1.0433 -0.033351
% replace negative values in NOConc column of Holly_NOx with 0:
Holly_NOx.NOConc(Holly_NOx.NOConc < 0) = 0;
disp(Holly_NOx);
Time NOConc NO2Conc NOxConc ____________________ _______ ________ _________ 11-May-2022 20:36:01 0.56796 1.792 0.34045 12-May-2022 20:36:01 0.71024 -0.44977 2 13-May-2022 20:36:01 0 -0.96963 -1.1307 14-May-2022 20:36:01 0 1.1944 -0.14942 15-May-2022 20:36:01 0.32287 -0.67921 0.20059 16-May-2022 20:36:01 0 2.5576 1.0473 17-May-2022 20:36:01 0 1.117 1.023 18-May-2022 20:36:01 0.82632 -1.0433 -0.033351
% or replace negative values in *any* column of Holly_NOx with 0:
Holly_NOx{:,:}(Holly_NOx{:,:} < 0) = 0;
disp(Holly_NOx);
Time NOConc NO2Conc NOxConc ____________________ _______ _______ _______ 11-May-2022 20:36:01 0.56796 1.792 0.34045 12-May-2022 20:36:01 0.71024 0 2 13-May-2022 20:36:01 0 0 0 14-May-2022 20:36:01 0 1.1944 0 15-May-2022 20:36:01 0.32287 0 0.20059 16-May-2022 20:36:01 0 2.5576 1.0473 17-May-2022 20:36:01 0 1.117 1.023 18-May-2022 20:36:01 0.82632 0 0
% another way to replace all negative values with 0:
Holly_NOx{:,:} = max(0,Holly_NOx{:,:});
disp(Holly_NOx);
Time NOConc NO2Conc NOxConc ____________________ _______ _______ _______ 11-May-2022 20:36:01 0.56796 1.792 0.34045 12-May-2022 20:36:01 0.71024 0 2 13-May-2022 20:36:01 0 0 0 14-May-2022 20:36:01 0 1.1944 0 15-May-2022 20:36:01 0.32287 0 0.20059 16-May-2022 20:36:01 0 2.5576 1.0473 17-May-2022 20:36:01 0 1.117 1.023 18-May-2022 20:36:01 0.82632 0 0

댓글 수: 7

Voss
Voss 2022년 6월 9일
("Answer" from @Kaiya Shealy moved here:)
This works!! Thank you very much!
Voss
Voss 2022년 6월 9일
편집: Voss 2022년 6월 9일
You're welcome! Any questions, let me know.
After running the code a few times, each time it gives different values in the Holly_NOx table, any reason why or any fix?
Voss
Voss 2022년 6월 9일
The code here is generating a timetable with random data, and then showing a few ways to replace negative data in the table - either in one column or all columns. You should pick the command you want to use and apply it to your table, i.e., don't create the random data each time, which was only for demonstration.
Gotcha, thank you again!
Voss
Voss 2022년 6월 9일
You're welcome! Any other questions, let me know.
Voss gets points for using braces on a table to modify the values. For more context on that, see the Arithmetic on Table Variables section of this example from the doc.
Another way to do this would be
Holly_NOx = varfun(@(x)max(x,0),Holly_NOx);
For large timetables, that's likely to be faster.

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

추가 답변 (0개)

카테고리

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

제품

릴리스

R2021b

질문:

2022년 6월 9일

댓글:

2022년 6월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by