data loop for matlab

1 view (last 30 days)
zakary Surprenant
zakary Surprenant on 17 Nov 2020
Edited: Adam Danz on 8 Dec 2020
for i= 1977:2012
for j=1:12
for k=1:31
mx=min(q1,[],'omitnan');
end
end
end
  4 Comments
Adam Danz
Adam Danz on 18 Nov 2020
How are your data arranged? Could you show us a sample?

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 18 Nov 2020
  1. Read data in as a table (see readtable)
  2. Convert the year-month-day columns to a single datetime vector and convert the table to a timetable
  3. Use retime to get the monthly max
Demo:
% Create demo table
T = table([1977;1977;1977;1977;1978;1978],[11;11;12;12;1;1],[1;2;1;2;1;2],(2:7)','VariableNames',{'Year','Month','Day','MaxTemp'})
T = 6x4 table
Year Month Day MaxTemp ____ _____ ___ _______ 1977 11 1 2 1977 11 2 3 1977 12 1 4 1977 12 2 5 1978 1 1 6 1978 1 2 7
% Convert to datetime values
TT = timetable(datetime(T.Year,T.Month,T.Day),T.MaxTemp,'VariableNames',{'MaxTemp'})
TT = 6x1 timetable
Time MaxTemp ___________ _______ 01-Nov-1977 2 02-Nov-1977 3 01-Dec-1977 4 02-Dec-1977 5 01-Jan-1978 6 02-Jan-1978 7
% Get monthly max
TT_MonthlyMax = retime(TT,'Monthly','max')
TT_MonthlyMax = 3x1 timetable
Time MaxTemp ___________ _______ 01-Nov-1977 3 01-Dec-1977 5 01-Jan-1978 7
If any month is missing from the data, the MaxTemp for that month will be NaN. Those rows can be removed with,
TT_MonthlyMax(isnan(TT_MonthlyMax.MaxTemp),:) = [];
  20 Comments
Steven Lord
Steven Lord on 8 Dec 2020
An empty cellstr is by {0x0 char} whereas a missing cellstr could be represented by {''} or perhaps {NaC} (Not-a-char, when the entire char array is 'missing') which would be consistent with cell arrays of doubles (NaN) or datetimes (NaT), etc.
Instead of trying to find some way to represent missing data in a cellstr, I probably would store my text data (including missing) in a string array and use the string array instead of a cellstr.
A = ["apple"; "banana"; "cherry"; missing; "eggplant"]
A = 5×1 string array
"apple" "banana" "cherry" <missing> "eggplant"
Adam Danz
Adam Danz on 8 Dec 2020
Edited: Adam Danz on 8 Dec 2020
Thanks, Steven Lord, strings do solve that problem and they distinguish between empties and missing.
A = ["apple"; ""; "cherry"; missing; "eggplant"]
A = 5×1 string array
"apple" "" "cherry" <missing> "eggplant"
But the bigger picture I'd like to emphasize is the option to treat empties as missing in the fillmissing(), rmmissing() etc family of functions. The current workaround is to find the empty arrays and replace them with missing values and for most data types, the missing() function comes in handy to fill with <missing> but missing() does not support cell arrays. So I'm suggesting that the _missing() family of functions optionally treat empty arrays as missing and to do this for empty cells as well.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!