How to add date,time corresponding to some incrementing count data in csv Excel file with help of MATLAB.

1 visualizzazione (ultimi 30 giorni)
I have csv file in Excel having data on weekly basis,but it is having date and time information in terms of incrementing count not in exactly date and time format.How to add date,time corresponding to incremental count data in csv Excel file with help of MATLAB.
  1 Commento
Dyuman Joshi
Dyuman Joshi il 15 Feb 2024
What is the format of the data stored? And what is the expected output?
If you can, share the file (Use the paperclip button to attach), so that we have a better understanding of the structure of the data, and provide suggestions/solutions accordingly.

Accedi per commentare.

Risposte (2)

Avni Agrawal
Avni Agrawal il 15 Feb 2024
Hi @NITI,
I understand that you are trying to add date & time that includes an incrementing count representing time intervals (for example, weeks). You can do this in MATLAB by using the `datetime` function to create a starting point and then add the increments to it.
Here is a step-by-step approach to achieve this:
1. Read the CSV File: Use `readtable` to read the CSV file into MATLAB as a table.
2. Define the Start Date: Determine the start date and time for your data.
3. Create the Date Vector: Use the incrementing count to create a vector of `datetime` objects.
4. Add the Date Vector to the Table: Append the date vector to your table.
5. Write the Updated Table to a New CSV File: Use `writetable` to write the updated table back to a new CSV file.
Here is an example MATLAB code snippet that demonstrates this process:
% Read the CSV file into a table
filename = 'your_data.csv'; % Replace with your actual file name
data = readtable(filename);
% Define the start date and time (replace with your actual start date and time)
startDate = datetime(2021, 01, 01, 00, 00, 00); % Year, Month, Day, Hour, Minute, Second
% Assume the incrementing count represents weekly intervals
% Create a vector of datetimes corresponding to each increment
% Replace 'IncrementColumn' with the name of your increment column
timeIncrements = data.IncrementColumn; % This is the column with the incrementing count
dateVector = startDate + weeks(timeIncrements - 1); % Subtract 1 if count starts at 1
% Add the date vector as a new column to the table
data.DateTime = dateVector;
% Write the updated table to a new CSV file
newFilename = 'updated_data.csv'; % Replace with your desired new file name
writetable(data, newFilename);
Make sure to replace `'your_data.csv'`, `'IncrementColumn'`, and the `startDate` with the actual file name, column name, and start date that apply to your data. Also, if your increments represent a different time interval than weeks, you can use the appropriate function like `days`, `hours`, `minutes`, etc., instead of `weeks`.
I hope this helps!
  1 Commento
NITI
NITI il 15 Feb 2024
hii Avni,
thanks for ur answer,I tried this code but couldn't go thru.pls see attached error msg snapshot.
Also attached is sample .csv file,what exactly I want is to insert date time wrt to count values.
Also note that this CSV file contains data for 3 hours on a particular date ,so I have 8 files for a day like this and have to add time stamping for all these 8 files for plotting purpose wrt time.
I hope this elaborates my query to get more specific solutions.

Accedi per commentare.


Star Strider
Star Strider il 15 Feb 2024
You can tell the datetime function how to interpret the available information with the 'InputFormat' name-value pair. See the documentation section on Format for details on how to code it, and Date and Time from Character Vectors for an example of how to use it.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by