Why won't Matlab recognize my data?
조회 수: 5 (최근 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에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!