how to convert a matix in .xlx specific julian day format?

1 view (last 30 days)
The .mat file contains the data of max and min temperature. 1977 is the year 1 to 31 are the days, column 2to 13 have the max and min data of january to june (column 2 tmax, column 3 tmin of january). same furhter 6 months data is repeated at the bottom of matrix below. How can i convert this to 01/01/1977 to 12/31/1997 in 3 columns. first column as date,2nd as tmax and third as tmin. i have to repeat this process for multpile year like 1920 to 2015

Answers (1)

Arun
Arun on 19 Oct 2023
Hey Mohsin,
I understand you would like to rearrange the ‘tmax’ and ‘tmin’ data along with the corresponding dates, where the data should be in ‘mm/dd/yyyy’ format. Currently, the data is organized into two sets, each containing data for six months.
setDefaultFormats” function can be used to set the datetime to required format.
Here is a sample code that you can use to arrange the data to as required:
% get date for column 1 for required data
datetime.setDefaultFormats('defaultdate','MM/dd/yyyy')
startDate = datetime(1977,1,1);
endDate = datetime(1977,12,31);
dateValues = (startDate:endDate)';
% Extract data
% get column 2 & 3 for the required data
tempValues = [0 0]; %temprature values for 6 months
tempValues = double.empty(tempValues);
% random Sample data representing your 13 columns
filedata = randi(12,31,13);
for i=2:2:12
tempValues=[tempValues;filedata(:,i:i+1)];
end
%similarly calculate the temp2Values for the next six months using correct row number
%I am using the same values for next six months
temp2Values=tempValues;
tempCombined=[tempValues;temp2Values]; %data for 12 months
T=table;
T=addvars(T,dateValues);
T=addvars(T,tempCombined(1:365,1));
T=addvars(T,tempCombined(1:365,2));
T.Properties.VariableNames = ["Date","tmax", 'tmin'];
%Output the data
T
T = 365×3 table
Date tmax tmin __________ ____ ____ 01/01/1977 1 1 01/02/1977 8 6 01/03/1977 11 8 01/04/1977 3 2 01/05/1977 3 9 01/06/1977 12 11 01/07/1977 12 3 01/08/1977 6 7 01/09/1977 12 10 01/10/1977 10 8 01/11/1977 7 4 01/12/1977 2 2 01/13/1977 5 11 01/14/1977 9 9 01/15/1977 5 11 01/16/1977 10 1
For doing it for multiple years, use this code as a function with datafile and year as input and the result as output.
Please refer the shared MathWorks documentation link for more setDefaultFormats” related details: https://www.mathworks.com/help/matlab/matlab_prog/set-display-format-of-date-and-time-arrays.html
Hope this helps.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!