Get datetime from variable name

조회 수: 3 (최근 30일)
Marcus Johnson
Marcus Johnson 2023년 11월 1일
편집: Jon 2023년 11월 1일
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
Jon
Jon 2023년 11월 1일
편집: Jon 2023년 11월 1일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2023년 11월 1일
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
.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by