MATLAB QUESTION How can I make a list of dates imported from EXCEL to be display
ID: 3885896 • Letter: M
Question
MATLAB QUESTION
How can I make a list of dates imported from EXCEL to be displayed on a matlab graph as month and year.
%% Import data from spreadsheet
% Script for importing data from the following spreadsheet:
%
% Workbook:
% /Users/joshuahansen/Downloads/Assignment2_8761720_1947-1979.xlsx
% Worksheet: Sheet1
%
% To extend the code for use with different selected data or a different
% spreadsheet, generate a function instead of a script.
% Auto-generated by MATLAB on 2017/09/14 11:03:21
clear all;
%% Import the data
[~, ~, raw] = xlsread('/Users/joshuahansen/Downloads/Assignment2_8761720_1947-1979.xlsx','Sheet1');
raw = raw(2:end,:);
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
%% Create output variable
data = reshape([raw{:}],size(raw));
%% Allocate imported array to column variable names
Year1 = data(:,1);
Month1 = data(:,2);
MSL1 = data(:,3);
Date = data(:,4);
%% Clear temporary variables
clearvars data raw R;
%%
x = Date;
y = MSL1;
T = readtable('/Users/joshuahansen/Downloads/Assignment2_8761720_1947-1979.xlsx');
% T.Date = datetime(x,'ConvertFrom','excel') % I found this online, however I dont know how to make it work.
dn=datetime(x,'InputFormat','MM/dd/yyyy');
format LONG;
b1 = xy
X = [ones(length(x),1) x];
b = Xy
yCalc1 = b1*x;
yCalc2 = X*b;
plot(x,yCalc2,'--')
legend('Data','Slope','Slope & Intercept','Location','best');
scatter(x,y)
hold on
plot(x,yCalc1)
xlabel('Date')
ylabel('MSL')
title('Linear Regression Relation Between Date and MSL')
grid on
Explanation / Answer
%% Import data from spreadsheet % Script for importing data from the following spreadsheet: % % Workbook: Libro2.xlsx % Worksheet: eventosPCL % % To extend the code for use with different selected data or a different % spreadsheet, generate a function instead of a script. % Auto-generated by MATLAB on 2015/12/14 11:16:39 %% Import the data, extracting spreadsheet dates in Excel serial date format [~, ~, raw, dates] = xlsread('Libro2.xlsx','eventosPCL','','',@convertSpreadsheetExcelDates); raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''}; dates = dates(:,1); %% Replace non-numeric cells with NaN R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells raw(R) = {NaN}; % Replace non-numeric cells R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),dates); % Find non-numeric cells dates(R) = {NaN}; % Replace non-numeric Excel dates with NaN %% Create table Libro2_ = table; %% Allocate imported array to column variable names Libro2_.FECHA = datetime([dates{:,1}].', 'ConvertFrom', 'Excel', 'Format', 'MM/dd/yyyy'); % For code requiring serial dates (datenum) instead of datetime, uncomment % the following line(s) below to return the imported dates as datenum(s). % Libro2_.FECHA=datenum(Libro2.FECHA); %% Clear temporary variables clearvars FECHA raw dates R;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.