MATLAB: How to save the data when we are using dir(‘*.hdf’)

file opening using .dir

I have opened all files using MOD= dir('*.hdf'); but cannot able to use this to open these files or to extract some values form these files. Here is my script
MOD= dir('*.hdf');
for i=1:365
Name=MOD(i).name
if i ==1
AE(i)=hdfread(Name,'/mod08/Data Fields/Angstrom_Exponent_Land_Mean', 'Index', {[1 1],[1 1],[180 360]});
AOD(i)=hdfread(Name, '/mod08/Data Fields/Optical_Depth_Land_And_Ocean_Mean', 'Index', {[1 1],[1 1],[180 360]});
else
AE(:,:,i)=hdfread(Name, '/mod08/Data Fields/Angstrom_Exponent_Land_Mean', 'Index', {[1 1],[1 1],[180 360]});
AOD(:,:,i)=hdfread(Name, '/mod08/Data Fields/Optical_Depth_Land_And_Ocean_Mean', 'Index', {[1 1],[1 1],[180 360]});
end
end
When I run this program, there is an error showing, Subscripted assignment dimension mismatch.
Error in test2 (line )
AE(i)=hdfread(MOD(i).name
,'/mod08/Data
Fields/Angstrom_Exponent_Land_Mean',
'Index', {[1 1],[1 1],[180 360]});

Best Answer

While not your specific problem (at least yet), using the magic number of 365 for the loop isn't good practice; use
for i=1:length(MOD) % iterate over returned file directory structure
Also, there's no need to create a second variable; just use hdfread(MOD(i).name, ... as you did in the code that generated the error instead of that as you posted; it has nothing to do with the error as you discovered.
AE(i)=hdfread(MOD(i).name, ...
tries to assign the entire output of the dataset read into a single element of a double array. That, needless to say, won't fit if there's more than a single value in the dataset. Use
AE=hdfread(MOD(i).name, ...
to return the dataset to the variable AE but then process that set of data before moving on to the next. If you must have more than a single set in memory at the same time, you'll have to do something to prevent overwriting subsequent values into the same array -- one way to do this would be to use a cell array as
AE{i}=hdfread(MOD(i).name, ...
where the "curlies" on the subscript create a cell array. You then would have to dereference those cell array references to get access to the data content itself.
Knowing nothing of the dataset nor what is intended to be done, we can't help in constructing an efficient way to process the data but the above will fix the immediate problem of reading the file(s).
Related Question