Error using str2double on a cell array, getting NaN
2 views (last 30 days)
Show older comments
I am trying to make a histogram of the frequency of safety violations, I am getting the data from an excel sheet that contains information like the location, time, and other descriptive information of the incidents. I pulled the time out to be able to plot but using the function I got NaN, the plot of course showed nothing.
ChillicotheInjury.RecordNo_=num2cell(ChillicotheInjury.RecordNo_)
a=class(ChillicotheInjury.Time)
b=str2double(ChillicotheInjury.Time(1))
c=ChillicotheInjury.Time(1)
d=class(i)
plot(1,i)
What could I do to fix this issue? I was told try using xlsread but when I did I got NaN and the date, it shows the day in dd/mm/yyyy format at the hour, column was all divided together.
a='cell' b=NaN c={'07:30:00'} d='double'
the rest of my code
SafetyDataOne=readtable('Incidents - Franklin Park Chillicothe Waynesville.xlsx');
SafetyDataTwo=readtable('Injuries - Franklin Park Chillicothe Waynesville.xlsx');
%Above lines import two excel sheets
SafetyDataTwo=SafetyDataTwo(:,[1:5]);
%Remove unnecceary columns
SafetyData=[SafetyDataOne;SafetyDataTwo];
%Combine into single table
d=SafetyData.Date;
%Date column is put into a varible
d.Format='dd-MMM-yyyy';
Date = cellstr(d);
d.Format = 'hh:mm:ss';
Time = cellstr(d);
%Break time column into more useful two columns
SafetyData=[SafetyData(:,1:2),Date,Time,SafetyData(:,4:5)];
% Put it together again
SafetyData.Properties.VariableNames([3 4])={'Date' 'Time'};
%Name the new columns
SafetyData=sortrows(SafetyData);
%Sort location column alphebetically
Location=SafetyData.Location;
SafetyDataChillicothe=sortrows(SafetyData(1:248,:),5);
SafetyDataFranklinPark=sortrows(SafetyData(249:454,:),5);
SafetyDataWaynesville=sortrows(SafetyData(455:end,:),5);
%Sort alpabetically the type of injury experinced
CillicotheFirstAid=SafetyDataChillicothe(1:225,:);
ChillicotheMFAR=SafetyDataChillicothe(225:229,:);
ChillicotheOther=SafetyDataChillicothe(230:232,:);
ChillicotheProperty=SafetyDataChillicothe(233:243,:);
ChillicotheInjury=SafetyDataChillicothe(244:248,:);
%Create Variables of the different types of injuries/incidents
FranklinEnvironmental=SafetyDataFranklinPark(1:2,:);
FranklinFirstAid=SafetyDataFranklinPark(3:103,:);
FranklinMFAR=SafetyDataFranklinPark(104:109,:);
FranklinOther=SafetyDataFranklinPark(110:138,:);
FranklinProperty=SafetyDataFranklinPark(139:188,:);
FranklinInjury=SafetyDataFranklinPark(189:199,:);
FranklinVehicle=SafetyDataFranklinPark(199:206,:);
%Create Variables of the different types of injuries/incidents
WaynesvilleFirstAid=SafetyDataChillicothe(1:74,:);
WaynesvilleMFAR=SafetyDataChillicothe(75:80,:);
WaynesvilleOther=SafetyDataChillicothe(81:84,:);
WaynesvilleProperty=SafetyDataChillicothe(85:87,:);
WaynesvilleInjury=SafetyDataChillicothe(88:94,:);
%Create Variables of the different types of injuries/incidents
ChillicotheInjury.RecordNo_=num2cell(ChillicotheInjury.RecordNo_)
a=class(ChillicotheInjury.Time)
b=str2double(ChillicotheInjury.Time(1))
c=ChillicotheInjury.Time(1)
d=class(i)
plot(1,i)
%histogram(table2array(ChillicotheInjury(:,4)))
% data_numNEW = table2array(ChillicotheInjury);
% data_numNEW = cellfun(@string,data_numNEW,'uni',0);
% data_numNEW = cellfun(@double,data_numNEW,'uni',0);
% data_numNEW = cell2mat(data_numNEW);
%ChillicotheInjury=class((ChillicotheInjury(:,4)))
%histogram(table2array(ChillicotheInjury(:,4)))
2 Comments
the cyclist
on 6 Aug 2021
Can you upload all the data and code needed to replicate the problem? It's very difficult to debug issues like this without that.
Answers (1)
Drishan Poovaya
on 10 Aug 2021
I understand you want to convert strings of date/time values into doubles so you can plot them. As per the image you attached, the format of the date/time is dd/mm/yyyy hh:mm.
As per the documentation of str2double , if str2double cannot convert text to a number, then it returns a NaN value which could be cause of the error you are facing
Rather than converting it to a double I would suggest you convert it into a datetime object, which makes it easy to get a plot of dates vs violations. Assuming you have all your date/time values stored in a cell array, the below code will convert them into a datetime array which can be used as the x axis vector for your plots
DtArray = datetime(ChillicotheInjury.Time, 'InputFormat', 'dd/MM/yyyy hh:mm');
DateTime doc :
https://www.mathworks.com/help/matlab/ref/datetime.html?s_tid=doc_ta#d123e285446
0 Comments
See Also
Categories
Find more on Calendar 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!