- Remove unnecesary string conversion by directly passing raw.textdata to datetime:
trying to extract the months to create a format looks like 202404, how to do that ?
65 views (last 30 days)
Show older comments
Date=datetime(raw.textdata(2:end,1),'InputFormat','dd-MMM-yy');%convert the data to date time
yr = year(Date); % Extract the year
mnth = month(Date); % Extract the month
% Create an array for the year-month combinations
yr_Month = yr * 100 + mnth;%example 202004
% Get unique year-month combinations
u_Months = unique(yr_Month);
%create month label in 'MMM yy" format
m_Labels = strings(length(u_Months), 1);
% Initialize vectors for monthly sums
m_sum_Production = zeros(length(u_Months), 1);
m_sum_T_Consumption = zeros(length(u_Months), 1);
m_sum_O_Consumption = zeros(length(u_Months), 1);
% Calculate the sum for each unique month-year combination
for i = 1:length(u_Months)
cur_Month = yr_Month == u_Months(i); % Logical index for the current month-year combination
m_sum_Production(i) = sum(Production(cur_Month)); % Monthly sum of Production
m_sum_T_Consumption(i) = sum(T_Consumption(cur_Month)); % Monthly sum of Total Consumption
m_sum_O_Consumption(i) = sum(O_Consumption(cur_Month)); % Monthly sum of Own Consumption
% Generate the corresponding 'MMM yy' label
yr_Part = floor(u_Months(i) / 100); % Extract the year
m_Part = mod(u_Months(i), 100); % Extract the month
% Create a datetime object for the first day of the month
m_Date = datetime(yr_Part, m_Part, 1);
% Format month as 'MMM yy'
m_Labels(i) = datetime(m_Date, 'mmm yy'); % Create the 'MMM yy' label
end
code is above
Unable to use a value of type datetime as an index.
Error in Q3 (line 15)
mnth = month(Date); % Extract the month
error is above
0 Comments
Answers (4)
Zinea
on 9 Oct 2024 at 10:06
Edited: Zinea
on 9 Oct 2024 at 10:09
The error above occurs due to the attempt to use a datetime object as an index, which is not allowed. The issue arises from the month function call when it tries to process the Date variable. To resolve this, the following changes need to be made:
Date = datetime(raw.textdata(2:end, 1), 'InputFormat', 'dd-MMM-yy');
2. Use datestr to directly format the date into 'MMM yy':
m_Labels(i) = datestr(m_Date, 'mmm yy'); % Create the 'MMM yy' label
Given below is the output after the above corrections:
2 Comments
Stephen23
on 9 Oct 2024 at 10:21
"Use datestr..."
DATESTR is deprecated, as its documentation makes very clear:
It is superfluous anyway, the datetime will get automagically converted when allocated to the string array:
str = "";
str(1) = datetime('now', 'Format','MMM yy')
Stephen23
on 9 Oct 2024 at 12:46
Edited: Stephen23
on 9 Oct 2024 at 13:00
s = ""; % your string array
dt = datetime('now')
dt.Format = 'MMM yy'; % the format given in your example code
s(1) = dt
dt.Format = 'yyyyMM'; % the format given in your title
s(1) = dt
If you want to avoid implicit type conversion:
s(1) = compose("%u%02u",dt.Year,dt.Month)
0 Comments
Steven Lord
on 9 Oct 2024 at 13:48
I would rename the variable you've created called month, as it will prevent you from calling the month function while it exists (or at all in a function, if you define the variable inside that function.)
dt = datetime("today")
callMonthFunction = month(dt) % 10
month = 42 % define month variable
y = month(dt) % error, not 10, as you can't index into the variable with today.
0 Comments
Seth Furman
on 18 Oct 2024 at 20:36
The string function takes a format argument. We can specify adjacent year and month in the format with "uuuuMM".
string(datetime(2024,4,1), "uuuuMM")
0 Comments
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!