How do I extract the Datetime array from the CSV ?

6 次查看(过去 30 天)
I am trying to extract the Average Ice concentration and date time from the CSV file. I managed to extract the Average ice concentration, but for the date time it is extracting as individual cell arrays with datetime in eah cell as follows
I would want it to extract the datetime like this (from another project but cell2mat doesnot work here)
The cell2mat function doesnot work. I would want some recommendations on how to proceed.
The code is as follows
%% Importing data file
% Note: The text file has headers and two columns:
% Column 1 - example of entry: {[2010-12-16 0:00]}
% Column 2 - Ice concentration Min
% Column 3 - Ice concentration Average
% Column 4 - Ice concentration Max
%% Because these are two different data types, they need to be imported as a CELL ARRAY
B=readcell('Ice concentration data.csv'); % Note: The csv format is automatically recognized.
B(1:1,:) = []; % Removing top rows (headers)
%% From this array, we extract the Average Ice Concentration levels
% Average Ice Concentration levels - production of a one-column numeric matrix
average_ice_concentration_level = cell2mat(B(:,3));% Converting average ice concentration from numeric cell
% to numeric vector (matrix)
%% From the same array, we extract date, month, day
% Step 1: Production of a character vector matrix
date_ice_concentration = (B(:,1));

采纳的回答

Star Strider
Star Strider 2022-11-7
The readtable funciton imports it as a datetime array —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1184228/Ice%20concentration%20data.csv')
T1 = 412×4 table
DateTime Conc_Ice_Min Conc_Ice_Avg Conc_Ice_Max ________________ ____________ ____________ ____________ 2010-12-16 00:00 3 8.2262 10 2010-12-18 00:00 4 8.6094 10 2010-12-21 00:00 0 5.5628 10 2010-12-23 00:00 0 6.2192 10 2010-12-27 00:00 0 8.2719 10 2010-12-28 00:00 0 9.4814 10 2010-12-29 00:00 3 9.4074 10 2010-12-30 00:00 0 8.5246 10 2010-12-31 00:00 0 7.944 10 2011-01-02 00:00 0 5.9938 10 2011-01-03 00:00 0 5.0078 10 2011-01-04 00:00 0 8.0016 10 2011-01-05 00:00 0 8.8131 10 2011-01-06 00:00 0 8.3935 10 2011-01-07 00:00 0 8.8402 10 2011-01-08 00:00 3 9.1284 10
T1.DateTime
ans = 412×1 datetime array
2010-12-16 00:00 2010-12-18 00:00 2010-12-21 00:00 2010-12-23 00:00 2010-12-27 00:00 2010-12-28 00:00 2010-12-29 00:00 2010-12-30 00:00 2010-12-31 00:00 2011-01-02 00:00 2011-01-03 00:00 2011-01-04 00:00 2011-01-05 00:00 2011-01-06 00:00 2011-01-07 00:00 2011-01-08 00:00 2011-01-09 00:00 2011-01-10 00:00 2011-01-11 00:00 2011-01-12 00:00 2011-01-13 00:00 2011-01-14 00:00 2011-01-15 00:00 2011-01-16 00:00 2011-01-17 00:00 2011-01-18 00:00 2011-01-20 00:00 2011-01-20 00:00 2011-01-22 00:00 2011-02-10 00:00
VN = T1.Properties.VariableNames;
lgdstr = cellfun(@(x)strrep(x,'_','\_'), VN(2:end), 'Unif',0)
lgdstr = 1×3 cell array
{'Conc\_Ice\_Min'} {'Conc\_Ice\_Avg'} {'Conc\_Ice\_Max'}
figure
plot(T1.DateTime, T1{:,2:end})
grid
legend(lgdstr, 'Location','best')
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by