Effacer les filtres
Effacer les filtres

my code is not running

1 vue (au cours des 30 derniers jours)
Seemant Tiwari
Seemant Tiwari le 12 Août 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

Réponse acceptée

Chunru
Chunru le 12 Août 2021
Modifié(e) : Chunru le 12 Août 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

Plus de réponses (1)

Image Analyst
Image Analyst le 12 Août 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

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by