Why won't Matlab recognize my data?
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
econogist
am 15 Apr. 2022
Kommentiert: Walter Roberson
am 15 Apr. 2022
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 Kommentare
Akzeptierte Antwort
Voss
am 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 Kommentare
Weitere Antworten (1)
Walter Roberson
am 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 Kommentare
Walter Roberson
am 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})
Siehe auch
Kategorien
Mehr zu Line Plots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!