MATLAB Answers

problem in loading .mat file (Error using load)

조회 수: 104(최근 30일)
Tae Lim
Tae Lim 19 Jun 2019
편집: per isakson 22 Jun 2019
I am having a trouble loading a particular mat file in Matlab.
I have close to 30 mat files that I consecutively generated using one script (script A). I am loading each of these mat file in another script (script B) for further analysis. When I try to load one particular mat file in script B, I get the error that looks something like this:
Error using load
Cannot read file C:\Users\....\Filename_SC7_... .mat.
'SC7' indicates the 7th file I am reading. All mat files that I load have the same name scheme with the value following SC changing (e.g. ...SC1....mat, ...SC2....mat) I suspected the file being corrupt and regenerated the file but I am still getting the same error message. I used script A and script B before without any troubles in the past so I am not sure why I am getting this issue now. Pretty much the only thing I changed this time is the name scheme, but I have used similar name schemes in the past as well.
Any comments will be appreciated!

  댓글 수: 9

표시 이전 댓글 수: 6
Tae Lim
Tae Lim 21 Jun 2019
Stephen, please see my answers to your questions below:
how are you generating the filenames?
-> My script iteratively runs the code to generate results using for loops. Filename is generated at the beginning of the each iteration using the code below
ResultMatName = strcat('.\Results\Util_20181111_Tgt0.7\Util_Tgt',num2str(reduction_target),'_SC',num2str(run),'_dr',num2str(discountrate),'_20181111_test2.mat');
As you can see, the name of the file updates according to the values I assign to the variables. the variable 'run' updates at each iteration from 1 to 27 using for loop.
Under the folder where I save result files, I have a totall of 27 mat files whose name liik like below
  • Util_Tgt0.7_SC1_dr0.07_20181111
  • Util_Tgt0.7_SC2_dr0.07_20181111
  • ...
  • Util_Tgt0.7_SC27_dr0.07_20181111
They are all generated by running a single script that iteratively generates these 27 files. All of them open without problem except for the 7th file, whose name is 'Util_Tgt0.7_SC7_dr0.07_20181111'
As I look into the problematic file further, I noticed that the file becomes un-loadable in the middle of the script run. I was able to check the saved data in the file as I ran the script and didn't notice any error or weird things.. All data and data structure is still in the file. I just cannot load them afterwards.
Below I am sharing two version of the problematic ''Util_Tgt0.7_SC7_dr0.07_20181111.mat'. They are saved at two different points of the script run. Upon successful script run, this file will contain 37 sets of data; each set is iteratively generated. The first file contains first two sets of data and still opens. The second file contains first three sets of data and at this point becomes un-loadable. So this problem basically occurs upon or before saving the thrid set of data to the result file.
I appreciate all of your help!
Tae Lim
Tae Lim 21 Jun 2019
per isakson, I just fixed the issue. You should be able to access the file now. Thank you for pointing this out!

Sign in to comment.


per isakson
per isakson 21 Jun 2019
On Win10, R2018b I have
  • downloaded your two files from Box successfully
  • dragged the 'Util_Tgt0.7_SC7_dr0.07_20181111_test2_FirstThreeSets_UNloadable.mat' from the Current Folder pane to Workspace pane. Load failed. Interactive Open also failed
>> load('Util_Tgt0.7_SC7_dr0.07_20181111_test2_FirstThreeSets_UNloadable.mat')
Error using load
Cannot read file H:\m\cssm\Util_Tgt0.7_SC7_dr0.07_20181111_test2_FirstThreeSets_UNloadable.mat.
  • dragged the 'Util_Tgt0.7_SC7_dr0.07_20181111_test2_BAUonly.mat' from the Current Folder pane to Workspace pane. Load successful. Peek at Results
>> Results.BAU
ans =
struct with fields:
newbau: [14×96 double]
deadtotbau: [14×96 double]
retbau: [14×60×96 double]
rettotbau: [14×96 double]
totbau: [14×96 double]
CostNewCap_bau: [14×96 double]
CostNewFuel_bau: [14×96 double]
CostNewMaint_bau: [14×96 double]
CostRet_bau: [14×96 double]
CostOldFuel_bau: [14×96 double]
CostOldMaint_bau: [14×96 double]
newbau_cap: [14×96 double]
deadtotbau_cap: [14×96 double]
rettotbau_cap: [14×96 double]
totbau_cap: [14×96 double]
Ebau: [96×1 double]
linear_emcon_of_interest: [96×1 double]
EVcharegeEFsbau: [96×1 double]
retbyagebau: [96×60 double]
TotPrimaryEnergy_bau: [14×96 double]
CFnewPriE_Util: [14×96 double]
CFoldPriE_Util: [14×60×96 double]
oldbau: [14×60×96 double]
deadbau: [14×60×96 double]
discountrate: 0.07
totUtilcostmatbau: [96×3 double]
xbau: [81984×1 double]

  댓글 수: 3

Tae Lim
Tae Lim 21 Jun 2019
per isakson, thank you for taking a look. Yes, I tried loading these files using different machines and noticed the same issue. The file that does not open basically contains additional set of data that look pretty similar to what you see under Results.BAU. The data is there but I cannot open the file.
CF) After reading your comments I noticed that I mistakenly shared with you the file that has only the first set of data (BAU), when I intended to share the file that contains first two set of data (BAU, CAY2015). I made this correction in my previous comment.
per isakson
per isakson 21 Jun 2019
I've downloaded and loaded "FirstTwoSets" successfully.
>> load('Util_Tgt0.7_SC7_dr0.07_20181111_test2_FirstTwoSets.mat')
>> Results
Results =
struct with fields:
BAU: [1×1 struct]
CAY2015: [1×1 struct]
>> Results.CAY2015
ans =
struct with fields:
new: [14×96 double]
deadtot: [14×96 double]
rettot: [14×96 double]
tot: [14×96 double]
CostNewCap: [14×96 double]
CostNewFuel: [14×96 double]
CostNewMaint: [14×96 double]
CostRet: [14×96 double]
CostOldFuel: [14×96 double]
CostOldMaint: [14×96 double]
new_cap: [14×96 double]
deadtot_cap: [14×96 double]
rettot_cap: [14×96 double]
tot_cap: [14×96 double]
E: [96×1 double]
Ebau: [96×1 double]
linear_emcon_of_interest: [96×1 double]
AnnEm: [14×96 double]
E_Captured: [96×1 double]
EVchargeEFs: [96×1 double]
incrUtil_costs_by_type: [96×3 double]
incrUtilcostsyearly: [96×1 double]
retbyage: [96×60 double]
TotPrimaryEnergy: [14×96 double]
CFnewPriE_Util: [14×96 double]
CFoldPriE_Util: [14×60×96 double]
old: [14×60×96 double]
dead: [14×60×96 double]
ret: [14×60×96 double]
totUtilcostmat: [96×3 double]
R: [36×27 double]
exitflag: 1
x: [81984×1 double]
E_annualReduc: 0.010505
per isakson
per isakson 21 Jun 2019
I've no good proposals, however
  • In the File Exchange there are a few submissions that read mat-files with Java. See tag:"mat-file" . Maybe, one of these fails and returns a useful error message.
  • Save your data to a mat-file version 7.3. See MAT-File Versions. Try to read the mat-file with HFD5. Start with h5disp - Display contents of HDF5 file
  • Contact tech-support (if your license includes support)

Sign in to comment.



Translated by