Problem with refrencing a table with brackets
Show older comments
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 Comments
What exactly do you want to access by this command -
sensors.Y_value{1}(4:12)
When you use dot-indexing on a table column, you get the column as a separate variable with the datatype of that column. (see below)
'sensors.Y_value' will come out as a double (or single depending on the data type). How do you intend to use {} on a numeric array?
LastName = ["Sanchez";"Johnson";"Zhang";"Diaz";"Brown"];
Age = [38;43;38;40;49];
Smoker = [true;false;true;false;true];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
patients = table(LastName,Age,Smoker,Height,Weight,BloodPressure)
out = patients.Height
Raz Hershtik
on 10 Jun 2023
The code expects the "time" column/variable to be a cell array of character vectors. It also expects the timestamp to be specifically within characters 4-12.
Trying to use it on anything else will not work.
But all of those STR2DOUBLE calls hint that this file importing is not done very well. Most likely better use of the READTABLE options would be better: if you upload a sample file by clicking the paperclip button, then someone can show you a better way to import it.
Image Analyst
on 10 Jun 2023
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
on 17 Jul 2023
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.
Answers (0)
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!