Problem with refrencing a table with brackets
5 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm trying to understand a part of a code that I didn't write, and i would really appriciate if you might have any insigt on what the original writer meant to do.
The part of the code is regarding reading a table from a csv file:
%{
==========================================================================
--- Sensors Data: --------------------------------------------------------
==========================================================================
The following section imports the csv file that contains the rotation
sensor data.
=> sensors.time: contains the timestamp of the csv ( saved at the first cell
in column with the name Y.value)
=> sensors.X_value: contains all the values of rotation in dimention X
(Pitch)that was recorded during the video capturing.
=> sensors.Y_value: contains all the values of rotation in dimention Y
(Roll)that was recorded during the video capturing.
=> sensors.Z_value: contains all the values of rotation in dimention Z
(Yaw)that was recorded during the video capturing.
==========================================================================
%}
current_dir = pwd;
csvFiles = dir( fullfile(current_dir,'*.csv') );
if numel(csvFiles) ~= 0 %there is one+ "csv" files in the current directory
%----------------------------------------------------------------------
%-- find the most recent video file -----------------------------------
%----------------------------------------------------------------------
numOfCsvFiles = size(csvFiles,1);
fileNumber = numOfCsvFiles;
dateNum = csvFiles(fileNumber).datenum;
numOfCsvFiles = numOfCsvFiles - 1;
while (numOfCsvFiles>0)
if(csvFiles(numOfCsvFiles).datenum > dateNum)
fileNumber = numOfCsvFiles;
dateNum = csvFiles(fileNumber).datenum;
end
numOfCsvFiles = numOfCsvFiles -1;
end
% Now sensors contains the most recet csv file in the current directory
sensors = readtable(csvFiles(fileNumber).name);
%----------------------------------------------------------------------
7
else
msg = "Error! There is no csv file in the current directory";
error(msg);
end
sensorStartTime= datetime(sensors.Y_value{1}(4:12),'InputFormat','mm:ss:SSSS');
sensorStartTime.Format=('mm:ss:SSSS');
sensors([1,2],:) = [];
sensors.time=str2double(sensors.time);
sensors.X_value=str2double(sensors.X_value);
sensors.Y_value=str2double(sensors.Y_value);
sensors.Z_value=str2double(sensors.Z_value);
The CSV table is something like this:

The line that i dont understand is:
sensorStartTime= datetime(sensors.Y_value{1}(4:12),'InputFormat','mm:ss:SSSS');
While running im getting the following error:

I understand they are trying to set the sensorStartTime to a certine value from the array, but i dont understand the syntax and the error that i got. Do any of you have any idea what might be the problem?
Thank you!
5 个评论
Image Analyst
2023-6-10
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
so we can fix it for you.
Peter Perkins
2023-7-17
Stephen23 is right that sensors.Y_value{1}(4:12) expects Y_value to be a cell variable in a table, and some sort of text timestamp to be in chars 4:12 of the first cell. But the other code expects sensors.time to be a var in a table with a text timestamp in the 4th element, and apparently it's numeric. In fact, what you shared suggests that it has the value 0.1480. It's not clear why you would try to tell datetime to treat that as a text timestamp. Likewise the contents of Y_value.
Noone can help unless you clearly show what you have in a small concrete example.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
