Why won't Matlab recognize my data?
1 次查看(过去 30 天)
显示 更早的评论
I am trying to plot CSV data (attached) on Google search trends, but the date (year and month) is only appearing as "NaN." How do I plot this?
0 个评论
采纳的回答
Voss
2022-4-15
The answer depends on how you're reading the file and how you want to deal with the cells in column 2 that say "<1".
One way to do it using readtable:
T = readtable('multiTimeline.csv')
plot(datetime(T.Month,'InputFormat','yyyy-MM'),T.universalBasicIncome__UnitedStates_)
One way to do it using readcell:
C = readcell('multiTimeline.csv')
C([1 2],:) = []; % remove header rows
idx = ~cellfun(@isnumeric,C(:,2)); % replace "<1" or any other
C(idx,2) = {NaN}; % non-numeric entry with a NaN
plot(datetime(C(:,1),'InputFormat','yyyy-MM'),vertcat(C{:,2}));
There are other ways.
0 个评论
更多回答(1 个)
Walter Roberson
2022-4-15
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/965680/multiTimeline.csv';
opt = detectImportOptions(filename, 'PreserveVariableNames', true);
opt = setvartype(opt, 1, 'datetime');
opt = setvaropts(opt, 1, 'InputFormat', 'yyyy-MM');
T = readtable(filename, opt);
T(1:2,:)
2 个评论
Walter Roberson
2022-4-15
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/965680/multiTimeline.csv';
opt = detectImportOptions(filename, 'PreserveVariableNames', true);
opt = setvartype(opt, 1, 'datetime');
opt = setvaropts(opt, 1, 'InputFormat', 'yyyy-MM');
T = readtable(filename, opt);
plot(T{:,1}, T{:,2})
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!