Compare monthly value of previous years with current monthly value

2 views (last 30 days)
I have a 12X3 table e.g. for 2022 and i need to compare the monthly value of previous years with todays monthly value. So I have for the previous years a monthly excel file , where the first collumn is year, the scond the month number and the third one the values.
So for example, today is 05-03-2023, I created a char array with the current date ('05-03-2023'), and i need to scan the second column of the excel for the month (03), in order to select the corresponing to March value (from the third collumn).Any ideas how to implement this?

Answers (1)

Jaswanth
Jaswanth on 4 Jan 2024
Hi,
I understand that you would like to extract data from a table by identifying with the month of current date to compare the values. To accomplish this task in MATLAB, you can use the 'readtable' function to read the Excel file into a table, and then you can filter the data to find the corresponding value for the current month.
You can follow the steps below to implement the same:
  1. Read the Excel file into MATLAB as a table.
  2. Convert the current date into a month number.
  3. Filter the table to find the rows where the month number matches the current month.
  4. Extract the value(s) from the third column for the matching month.
The following example code snippet demonstrates the above specified process:
% Assuming the Excel file is named 'monthly_data.xlsx'
filename = 'monthly_data.xlsx';
% Read the Excel file into a table
dataTable = readtable(filename);
% Create a char array with the current date
currentDate = '05-03-2023';
% Convert the char array to a datetime object and extract the month number
currentMonth = month(datetime(currentDate, 'InputFormat', 'dd-MM-yyyy'));
% Filter the table to find the rows with the matching month
matchingRows = dataTable(dataTable.MonthNumber == currentMonth, :);
% Extract the values from the third column for the matching month
matchingValues = matchingRows.ValueColumn; % Replace 'ValueColumn' with the actual name of the third column
% Display the matching values
disp(matchingValues);
Hope the solution provided above is helpful.
Thanks!

Community Treasure Hunt

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

Start Hunting!