my code is not running

1 view (last 30 days)
Seemant Tiwari
Seemant Tiwari on 12 Aug 2021
Answered: Image Analyst on 12 Aug 2021
for year = 2009:2017
year index = year-2008
year struct = load( [ 'D:\DataStation\hr ',num2str(ye), '\mat\station.mat' ] );
for station = 1:30
eval ( [ "station double = year struct.station_',num2str(station),';'] );
station double = station double (1:8760,:);
data(:,:,station,year) = station double
end
end

Accepted Answer

Chunru
Chunru on 12 Aug 2021
Edited: Chunru on 12 Aug 2021
This is my best guess.
for year = 2009:2017
year_index = year-2008 % no space in variable names
year_struct = load( [ 'D:\DataStation\hr ',num2str(year), '\mat\station.mat' ] );
for station = 1:30
eval ( ['station_double = year_struct.station_', num2str(station), ';'] );
station_double = station_double (1:8760,:);
data(:, :, station, year_index) = station_double;
end
end

More Answers (1)

Image Analyst
Image Analyst on 12 Aug 2021
This is how I'd do it:
for theYear = 2009:2017
yearIndex = theYear-2008; % no space in variable names
fullFileName = sprintf('D:/DataStation/hr %d/mat/station.mat', theYear);
if isfile(fullFileName)
% Load .mat file into a structure.
yearStruct = load(fullFileName);
% Get all the field names 'station_1', 'station_2', etc. into a cell array
fn = fieldnames(yearStruct);
for station = 1 : length(fn)
% Get this particular field name.
thisFieldName = fn{station};
% Get the 2-D array in this field.
thisMatrix = yearStruct.(thisFieldName);
% Crop out part of it.
station_double = thisMatrix (1:8760,:);
% Add this matrix to our 4-D array.
data(:, :, station, yearIndex) = station_double;
end
else
warningMessage = sprintf('Warning: file not found:\n%s', fullFileName);
uiwait(warndlg(warningMessage));
end
end

Community Treasure Hunt

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

Start Hunting!