Import function automatically converting the datenum values

1 visualizzazione (ultimi 30 giorni)
I need the date read as 'dd/MM/yyyy hh:mm' and I keep getting this suggestion:
Warning: Successfully converted the text to datetime using the
format 'MM/dd/uuuu HH:mm', but the format is ambiguous and could
also be 'dd/MM/uuuu HH:mm'. To create datetimes from text with a
specific format call:
datetime(textinput,'InputFormat',infmt)
but I find myself unable to correctly implement the fix MatLab proposes. I am using a function I made using the import tool, you can see it below:
function puissance = V1_import_puissance(workbookFile, sheetName, startRow, endRow)
%%Input handling
% If no sheet is specified, read first sheet
if nargin == 1 || isempty(sheetName)
sheetName = 1;
end
% If row start and end points are not specified, define defaults
if nargin <= 3
startRow = 2;
endRow = 801; %to make sure I import a file regardless of how big it is. Will import a bunch of empty rows
end
%%Setup the Import Options
opts = spreadsheetImportOptions("NumVariables", 2);
% Specify sheet and range
opts.Sheet = sheetName;
opts.DataRange = "A" + startRow(1) + ":B" + endRow(1);
% Specify column names and types
opts.VariableNames = ["Time", "Puissance"];
opts.VariableTypes = ["datetime", "double"];
%This I where I have tried to implement it :
%datetime(opts,'InputFormat', 'dd/MM/yyyy hh:mm');
% Import the data
puissance = readtable(workbookFile, opts, "UseExcel", false);
for idx = 2:length(startRow)
opts.DataRange = "A" + startRow(idx) + ":B" + endRow(idx);
tb = readtable(workbookFile, opts, "UseExcel", false);
puissance = [puissance; tb]; %#ok<AGROW>
end

Risposta accettata

Guillaume
Guillaume il 12 Nov 2018
Your attempt, datetime(opts,'InputFormat', 'dd/MM/yyyy hh:mm'); tries to convert the import options to datetime which of course is not going to work. Instead, you need to tell the import options to import that datetime variable with your chosen format. This is done with setvaropts:
opts = setvaropts(opts, "Time", "InputFormat", "dd/MM/yyyy hh:mm");

Più risposte (0)

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by