Filter löschen
Filter löschen

my code is not running

1 Ansicht (letzte 30 Tage)
Seemant Tiwari
Seemant Tiwari am 12 Aug. 2021
Beantwortet: Image Analyst am 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

Akzeptierte Antwort

Chunru
Chunru am 12 Aug. 2021
Bearbeitet: Chunru am 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

Weitere Antworten (1)

Image Analyst
Image Analyst am 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

Kategorien

Mehr zu Numeric Types finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by