my code is not running

1 次查看(过去 30 天)
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

采纳的回答

Chunru
Chunru 2021-8-12
编辑:Chunru 2021-8-12
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

更多回答(1 个)

Image Analyst
Image Analyst 2021-8-12
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

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by