Why won't Matlab recognize my data?
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Voss
on 15 Apr 2022
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 Comments
More Answers (1)
Walter Roberson
on 15 Apr 2022
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 Comments
Walter Roberson
on 15 Apr 2022
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})
See Also
Categories
Find more on Spreadsheets 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!