Conver XLS to MAT (Error: File might be corrupt)

조회 수: 3 (최근 30일)
Danny Xu
Danny Xu 2019년 2월 28일
Hi Guys,
I am a very novice Matlab user. I haved recently installed R2018a on a new machine with Excel Version 1708 on Windows 10 Entreprise 1803. I was previously using R2016b. Since upgrading my machine/Matlab, the code has been failing with the below error:
Error using load
Unable to read MAT-file File.mat.
File might be corrupt.
I believe I have narrowed the cause to the code below. The purpose of the code is to convert data from Excel into Triangle Matlab format. However, the FILE.MAT is corrupt and cannot be used.
Does anyone know how to resolve this?
---------------------------MATLAB Code Below ---------------------------------------------
function fOutput = Triangle_data_import(fInputs,fTimeCalendar,dtProfileStartDate,dtMaxForecastEndDate,tDescription,fBase)
%% Import Input file (XLSX) to Triangle (MAT)
%% Load base
if ~strcmp(fBase,'');
fprintf('Loading: %s\n',fBase); %except passed variables
load(fBase, '-regexp', '^(?!fInputs$|fTimeCalendar$|dtProfileStartDate$|dtMaxForecastEndDate$|tDescription$|fBase$|tComponents$)\w');
end
%% Inputs file
fprintf('Opening %-14s ','Inputs'); tic;
bkInputs = matlab.io.spreadsheet.internal.createWorkbook('xlsx',fInputs,false);
fprintf('took %.1fsecs\n',toc);
fprintf('Importing: Evaluations, Discounts, RetailTariffs, NetworkTariffs, CustomerBase, ScenarioVolume, CostStack, CompetitorTariffs\n');
%tVersions: Version text -> Version Id
%tVersion = readtable(fInputs,'Sheet','Version');
%tPPC: XCODE -> State,Segment,Fuel,Network,price_plan_code
% Primary_NTC,CL_NTC,CS_NTC,Solar_NTC,Primary_RTC,CL_RTC,CS_RTC,Solar_RTC
% XCODE, ECODE
tEvaluations = readtable2(bkInputs,'Sheet','Evaluations');
tPPCDupes = unique(tEvaluations(:,{'XCODE','PPC','SolarFlag'})); %remove dupe (XCODE,PPC,SolarFlag)
tPPCDupes = tPPCDupes(~strcmp(tPPCDupes.PPC,'TBA'),:); %remove TBAs
[tPPC,iOld] = unique(tPPCDupes(:,{'PPC','SolarFlag'}));
tPPC.XCODE = tPPCDupes{iOld,'XCODE'}; %remove dupe (PPC,SolarFlag)
[~,idxs,~] = unique(tEvaluations.XCODE);
tEvaluations = tEvaluations(idxs,:); %grab only the first XCODE to evaluate
clearvars idxs iOld;
%tDiscounts: product_type_code,tco_code -> PPD
tDiscounts = readtable2(bkInputs,'Sheet','Discounts');
%tRetail: State,Segment,Fuel,Network,RetailTariff,Version -> revenue
tRetail = readtable2(bkInputs,'Sheet','RetailTariffs',2);
%tNetwork: State,Segment,Fuel,Network,NetworkTariff,Version -> cost
tNetwork = readtable2(bkInputs,'Sheet','NetworkTariffs',2);
%tComp: rows
tComp = readtable2(bkInputs,'Sheet','CompetitorTariffs',2);
%tCustomerBase: XCODE -> brand_code, netactives, PPD
tCustomerBase = readtable2(bkInputs,'Sheet','CustomerBase');
%tScnVol: StateSegFuel -> volume %first row is overwritten
tScenarioVolume = readtable2(bkInputs,'Sheet','ScenarioVolume');
%tCostStack: labels x Dates grid
tCostStack = readtable2(bkInputs,'Sheet','CostStack');
tCostStacklabels = tCostStack(:,1:15); %CAUTION! labels end
vCostStackDates = datetime(tCostStack.Properties.VariableNames(16:end),'InputFormat','''x''dd_MMM_yyyy'); %CAUTION! dates start
tCostStackDatevec = datevec(vCostStackDates)';
tCostStackvalues = tCostStack(:,16:end); %CAUTION! dates start
%% TimeCalendar file
if strcmp(fBase,'')
fprintf('Opening %-14s ','TimeCalendar'); tic;
bkTimeCalendar = matlab.io.spreadsheet.internal.createWorkbook('xlsx',fTimeCalendar,false);
% tElecProfile: Date -> profiles x23
tElecProfile = readtable2(bkTimeCalendar,'Sheet','ElectricityLoadProfiles');
[tElecProfile.Y,tElecProfile.M,tElecProfile.D,tElecProfile.H,tElecProfile.MN,tElecProfile.S]=datevec(tElecProfile.Time);
% tGasProfile: Date -> profiles x4
tGasProfile = readtable2(bkTimeCalendar,'Sheet','GasLoadProfiles');
tGasProfile.Time=datenum(tGasProfile.Time);
[tGasProfile.Y,tGasProfile.M,tGasProfile.D,tGasProfile.H,tGasProfile.MN,tGasProfile.S]=datevec(tGasProfile.Time);
% tPublicHolidays: State -> Dates. only used for time calender import
tPublicHolidays = readtable2(bkTimeCalendar,'Sheet','PublicHolidays');
% tETimeClass: Month,Day,IntervalStart,IntervalNum (48*48=2304) -> 466 time classes
tETimeClass = readtable2(bkTimeCalendar,'Sheet','ElecTimeClass',130);
tETimeClass.MDI= strcat(num2str(tETimeClass.Month),tETimeClass.Day,num2str(tETimeClass.Intervalnum));
% tGTimeClass: Month,Day,IntervalStart,IntervalNum (48) -> 37 time classes
tGTimeClass = readtable2(bkTimeCalendar,'Sheet','GasTimeClass',5);
tGTimeClass.MDI= strcat(num2str(tGTimeClass.Month),tGTimeClass.Day,num2str(tGTimeClass.Intervalnum));
fprintf('took %.1fsecs\n',toc);
% importProfiles ElecTimeClass&GasTimeClass and precompute intervals
fprintf('Importing %-14s ','Profiles'); tic;
[tECalendar,tEIntervalCounts, tGCalendar,tGIntervalCounts] ...
= importProfiles(dtProfileStartDate,dtMaxForecastEndDate,tPublicHolidays,tETimeClass,tGTimeClass);
fprintf('took %.1fsecs\n',toc);
end
%% save
fOutput=['..\Data\' 'Triangle_data_' datestr(date,'yyyymmdd') '_' strrep(tDescription,' ','')];
if exist([fOutput '.mat'], 'file') == 2
fprintf('Overwriting: %s\n',fOutput);
else
fprintf('Saving to: %s\n',fOutput);
end
clearvars suff description onlyUpdateRates bkInputs bkTimeCalendar fBase; %variables that should not be saved
save(fOutput);
fprintf('\n');
end
%%
function t = readtable2(book,~,sheet,startRow)
%Here be dragons.
% from: C:\Program Files\MATLAB\R2016b\toolbox\matlab\datatypes\@table\readXLSFile.m
% TODO: allow table.readXLSFile to be called directly
% c = ?table;
% m = findobj(c.MethodList,'Name','readXLSFile');
% f = @table.readXLSFile;
% p = findprop(m,'Access');
if ~exist('startRow','var'); startRow = 1; end;
oSheet = book.getSheet(sheet);
rangeText = matlab.io.spreadsheet.internal.usedDataRange(oSheet);
rangeNum = oSheet.getRange(rangeText,false); %[startRow, startCol, numRows, numCols]
rangeNum(3) = rangeNum(3) - (startRow - rangeNum(1));
rangeNum(1) = startRow;
rdOpts.file = book;
rdOpts.format = 'xlsx';
rdOpts.sheet = sheet;
rdOpts.range = rangeNum; %[startRow, startCol, numRows, numCols]
rdOpts.readVarNames = true; %auto converts types
rdOpts.basic = false;
rdOpts.treatAsEmpty = cell(0,1);
rdOpts.logicalType = 'logical';
rdOpts.textType = 'char';
rdOpts.datetimeType = 'datetime';
out = matlab.io.spreadsheet.internal.readSpreadsheetFile(rdOpts);
data = out.variables;
if isempty(data); t = table; return; end
% data = horzcat(data{:});
% data(startrow,:) = [];
%varNames = data(1,:);%out.varNames;
varNames = out.varNames;
if ~iscellstr(varNames); varNames = matlab.io.spreadsheet.internal.stringize(varNames, false); end
varNames = matlab.lang.makeValidName(varNames);
t = table(data{:},'VariableNames',varNames);
% data = data(2:end,:);
% %t = cell2table(data,'VariableNames',varNames); %does not convert types
% dataCellNest = cell(1,size(data,2));
% for i = 1:size(data,2); dataCellNest{i} = data(:,i); end
% t = table(dataCellNest{:},'VariableNames',varNames);
end

답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by