Get datetime from variable name

2 views (last 30 days)
Marcus Johnson
Marcus Johnson on 1 Nov 2023
Edited: Jon on 1 Nov 2023
Greetings,
I have multiple .csv files of the one that I added, where the first row contains a time and all the rows below it contains values. I use the following code to read all of them into Matlab:
for i = 1:length(Detector)
Detector_tbl{i} = readtable(Detector(i).name,'ReadVariableNames',true,'PreserveVariableNames',true);
end
Detecor_tbl then look like 'detector_tbl.png' with each cell looking like 'detector_tbl_cell.png'.
I then use the followng code to put all the data in a table:
for i = 1:583
Detector_values(:,i) = Detector_tbl{i};
end
Detector_values then look like 'detector_values.png'.
So my questions are:
  1. Why does only the first column in Detector_values get a time variable name even though all cells in Detector_tbl have a time variable name?
  2. Is there a way to turn the variable name time into datetime so I can use the time for each column instead of it only being the variable name?
  3. If the answer is no to question 2, is there a way to make it so the time from the .csv files doesn't become the variable name and instead becomes a datetime value in the first row when I import them into Matlab?
Thanks
  1 Comment
Jon
Jon on 1 Nov 2023
Edited: Jon on 1 Nov 2023
It looks like you have a sequence of vectors collected at time values given by the first row in your .csv file.
Tables store column oriented data where each element of the column is of the same type. You can't have a table where the first element of the column is a datetime and the other elements are doubles. So that approach will not work.
It also doesn't seem like a good idea to have columns of data, each named according to a time stamp value. If you do this then you would need to convert the character or string variable names, back into time values when you want to use them.
Determining a good way to store these values in MATLAB depends upon what you want to do with the data.
What do you actually want to do next with the data once you have imported it?

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 1 Nov 2023
When I look at the .png files, all the values appear to be the same among all the other variables. I am not certain what you want to do with those, and in any event, only one .csv file is provided.
Getting datetime arrays from the variable names is straightforward —
Detector = dir('*.csv');
for i = 1:length(Detector)
Detector_tbl{i} = readtable(Detector(i).name,'ReadVariableNames',true,'PreserveVariableNames',true);
VN(i) = Detector_tbl{i}.Properties.VariableNames;
DT(i) = datetime(VN{i}, 'InputFormat','HH:mm:ss.SSS', 'Format','HH:mm:ss.SSS')
end
DT = datetime
02:48:05.523
Detector_tbl{1}
ans = 40001×1 table
02:48:05.523 ____________ 1.4599 1.4599 1.4599 1.4599 1.4599 1.4599 1.4599 1.4599 1.4599 1.4599 1.4599 1.4599 1.4599 1.4599 1.4599 1.4599
.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!