Get datetime from variable name
조회 수: 3 (최근 30일)
이전 댓글 표시
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:
- 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?
- 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?
- 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
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
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
Detector_tbl{1}
.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!