-1

I am currently working on constructing my dataset after extracting signals from a (.mat) database in Matlab.

[X,Y]=extract_structures(filename);

time=X(1).Data;

time =

Columns 1 through 6

         0    0.0100    0.0200    0.0300    0.0400    0.0500

  Columns 7 through 12

    0.0600    0.0700    0.0800    0.0900    0.1000    0.1100

 Columns 5 119 through 5 124

   51.1803   51.1903   51.2003   51.2103   51.2203   51.2303

  Columns 5 125 through 5 127

   51.2403   51.2503   51.2603

The X representing time. I extracted this time variable and other signals and constructed my data vector, starting with the first column as time, following this syntax :

data = [time' variable_u' variable_y'....... ];

To be able to use the csv file in Python my dataset should have a date column that contains the datetime of the time-series.

How can I achieve the transformation of the 'time' column into a datetime format using Matlab before exporting the csv file into Python.

6
  • We don't have your mat file, so we don't have any idea what data type time = X(1).Data is. Is it a MATLAB datetime? A duration? A string which looks like a datetime? A datenum? Take a look at what additional information we might need in a minimal reproducible example and then edit your question to include it. The fact you're starting from a mat file and targeting Python seems irrelevant here, to answer this question we just need details about (1) what data type you have in MATLAB and (2) how you want it to appear when written to csv, which has no sense of data types so it's just a formatted string
    – Wolfie
    Commented Jan 25, 2024 at 17:05
  • Hello, Thank your for your answer. The .mat file is a record of vehicle's dynamic simulation. And the vector time is a vector which contains the duration of the simulation record. And for your second question, I only have to respect the condition to be able to incorporate my dataset in an ML model, so, The dataset should be a csv file with a date column that contains the datetime of the time-series.
    – achour99
    Commented Jan 25, 2024 at 17:19
  • Your comment doesn't answer my query, what datatype or class is the "vector which contains the duration", and how do you expect to represent that as a date? Edit your question to include a reproducible example. You could invent a date column which is just "1/1/1970" if it's arbitrary, what's your specific issue just doing that?
    – Wolfie
    Commented Jan 25, 2024 at 17:32
  • The time is a 1x5127 double vector.
    – achour99
    Commented Jan 25, 2024 at 17:40
  • So what is your expected output in the csv? Does it relate to the time at all or is that irrelevant?
    – Wolfie
    Commented Jan 25, 2024 at 22:23

1 Answer 1

0

We dont have the mat file so this is following a guess. My guess is that what you are reading is datenum results , and that you need to convert them to the full datetime info. this can be done in matlab using

 [y,mo,d,h,mi,s] = datevec(input)

in Python there is a little more complexity:

from datetime import datetime, timedelta

def matlab_datenum_to_datevec(datenum):
    # MATLAB's datenum starts on January 0, 0000 which is -366 days from January 1, 0001
    # Python's datetime starts from January 1, 1970
    # Therefore, we need to find the difference in days between these two start dates
    # and then add this to the provided datenum

    matlab_start = datetime(1, 1, 1)
    python_start = datetime(1970, 1, 1)
    days_difference = (python_start - matlab_start).days

    # Convert MATLAB datenum to Python datetime
    python_datetime = matlab_start + timedelta(days=datenum - days_difference)

    # Convert to date vector [year, month, day, hour, minute, second]
    date_vector = [python_datetime.year, python_datetime.month, python_datetime.day,
               python_datetime.hour, python_datetime.minute, python_datetime.second]

   return date_vector

an example using this:

datenum_value = 738427  # This is an example datenum value
date_vector = matlab_datenum_to_datevec(datenum_value)
print(date_vector)

This function matlab_datenum_to_datevec takes a MATLAB datenum value as input and returns a date vector similar to MATLAB's datevec output. The difference between the MATLAB and Python date systems is accounted for in the calculation.

1
  • Hello, Many thanks for your providing. Therefore, the datevec is not solving ly issue. Because, in my case a time vector in seconds and want to convert it into a datenum so I will be able to respect the following condition "The dataset should be a csv file with a date column that contains the datetime of the time-series." So, I am trying to use datetime to create a datetime object using my time vector in seconds.
    – achour99
    Commented Jan 29, 2024 at 8:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.