Import function automatically converting the datenum values

4 ビュー (過去 30 日間)
Clara Scattolin
Clara Scattolin 2018 年 11 月 12 日
コメント済み: Clara Scattolin 2018 年 11 月 13 日
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

採用された回答

Guillaume
Guillaume 2018 年 11 月 12 日
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");
  1 件のコメント
Clara Scattolin
Clara Scattolin 2018 年 11 月 13 日
Thank you! I changed the double quotes for single quotes and it worked perfectly.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCalendar についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by