How can I loop this ?

조회 수: 1 (최근 30일)
janas
janas 2023년 12월 28일
댓글: janas 2023년 12월 29일
I want to change the stations for each different parameter in the loop, any help?
INPUT.IN={... %1-file Type | %2-file | 3-Location (Sea) | 4-Location (hunte) | 5-Location (weser) | 6-Location (Wuemme) | 7-Variable | 8-abv. | 9-abv | 10-Unit | 11-Kennnummer | 12-Prestr | 13-Poststr | 14-VariableLimits | 15-LoadTempData ? y=1
'.dat', '/net/themis/geo/messungen/jadeweser/loc/wasserstand', 'ALW', '', '', '', 'wasserstand', 'wl.', 'wl', 'm NHN', 3, '', ' ', [-4,4], 0;...
'.dat', '/net/themis/geo/messungen/jadeweser/loc/einleitungs-Volumenstrom', '', 'OWH', 'INT', {'HEX'; 'HELL'; 'GRAS'}, 'einleitungs-Volumenstrom', 'sv.', 'sv', 'm NHN', 1058, '', ' seeseitig', [-4,4], 1;...
'.dat', '/net/themis/geo/messungen/jadeweser/loc/Salzgehalt', 'ALW', '', 'HEM', '', 'Salzgehalt', 'sa.', 'sa', 'g/kg', 5, '', ' (2D) seeseitig', [30,35], 1;...
'.dat', '/net/themis/geo/messungen/jadeweser/loc/temperatur', 'ALW', '', 'INT', '', 'temperatur', 'te.', 'te', '°C', 6, '', ' (2D) seeseitig', [0,30], 1 ;...
'.dat', '/net/themis/geo/messungen/jadeweser/loc/truebung', 'DWG', '', 'INT', '', 'truebung', 'tu.', 'tu', 'm', 1161, '', '', [0,5], 0; ... '
};
for f=1:size(INPUT.IN,1)
parameters = INPUT.IN{f,7};
parameters_abv_1 = INPUT.IN{f,8};
parameters_abv_2 = INPUT.IN{f,9};
stations_sea = INPUT.IN{f,3};
stations_hunte = INPUT.IN{f,4};
stations_weser =INPUT.IN{f,5};
stations_wuemme =INPUT.IN{f,6};
kennnummers = INPUT.IN{f,11};
PATH_para = INPUT.IN{f,2};
[~, ~, start_end] = boe_info([PATH_para, '/', num2str(HYD_YEAR-1), '/boewrt/'], 'inputfile', [parameters_abv_1, stations_sea, '.000.', num2str(HYD_YEAR-1), '.boewrt.dat']);
[temp_stats, temp_year, start_end2] = boe_info([PATH_para, '/', num2str(HYD_YEAR), '/boewrt/'], 'inputfile', [parameters_abv_1, stations_sea, '.000.', num2str(HYD_YEAR), '.boewrt.dat']);
start_end = [start_end; start_end2];
[DATA, TIMES] = Year2Hydrolog(PATH_para, stations_sea, start_end, HYD_YEAR, kennnummers, parameters_abv_2);
  댓글 수: 2
Stephen23
Stephen23 2023년 12월 28일
Tip: replace string concatenation (using hard-coded file separators) with much better FULLFILE, i.e.:
[PATH_para, '/', num2str(HYD_YEAR-1), '/boewrt/']
should be
fullfile(PATH_para,num2str(HYD_YEAR-1),'boewrt')
janas
janas 2023년 12월 29일
thank you for your help

댓글을 달려면 로그인하십시오.

채택된 답변

Hassaan
Hassaan 2023년 12월 28일
@Anas In this revised script:
  1. We create a cell array of stations for each parameter.
  2. We use a nested loop to iterate through each station for the current parameter.
  3. For each non-empty station, we call boe_info and Year2Hydrolog to get the data and times, which we store in all_stations_data and all_stations_times.
  4. After processing all non-empty stations for a parameter, the data and times are available for further processing or storage.
Remember to define the boe_info and Year2Hydrolog functions before running this script, and make sure they are on the MATLAB path. This script assumes these functions are designed to handle the data extraction logic as per your needs. Here try this and let me know. Thanks.
HYD_YEAR = 2023; % replace 2023 with the actual year you are analyzing
for f = 1:size(INPUT.IN,1)
parameters = INPUT.IN{f,7};
parameters_abv_1 = INPUT.IN{f,8};
parameters_abv_2 = INPUT.IN{f,9};
stations = {INPUT.IN{f,3}, INPUT.IN{f,4}, INPUT.IN{f,5}, INPUT.IN{f,6}}; % Create a cell array of stations
kennnummers = INPUT.IN{f,11};
PATH_para = INPUT.IN{f,2};
% Initialize a cell array to hold data from all stations for the current parameter
all_stations_data = {};
all_stations_times = {};
% Iterate through each station, process non-empty ones
for station_idx = 1:length(stations)
station = stations{station_idx};
if ~isempty(station)
% Assuming boe_info and Year2Hydrolog are functions that return the data and time for a station
[~, ~, start_end] = boe_info([PATH_para, '/', num2str(HYD_YEAR-1), '/boewrt/'], 'inputfile', [parameters_abv_1, station, '.000.', num2str(HYD_YEAR-1), '.boewrt.dat']);
[temp_stats, temp_year, start_end2] = boe_info([PATH_para, '/', num2str(HYD_YEAR), '/boewrt/'], 'inputfile', [parameters_abv_1, station, '.000.', num2str(HYD_YEAR), '.boewrt.dat']);
start_end = [start_end; start_end2];
[DATA, TIMES] = Year2Hydrolog(PATH_para, station, start_end, HYD_YEAR, kennnummers, parameters_abv_2);
% Store the data and times from each station
all_stations_data{end+1} = DATA;
all_stations_times{end+1} = TIMES;
end
end
% At this point, all_stations_data and all_stations_times contain the
% collected data and times from all the non-empty stations for the current parameter.
% You can now process or save this data as needed.
end
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
  댓글 수: 1
janas
janas 2023년 12월 28일
this is working well but I am getting an error at one location where there is 3 stations added as cells, {'HEX'; 'HELL'; 'GRAS'}

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Hassaan
Hassaan 2023년 12월 28일
To accommodate this in your loop, you can set up a selection mechanism that chooses the non-empty station for each parameter. Here's how you can modify your loop. A initial idea of how you can implement it:
INPUT.IN={... %1-file Type | %2-file | 3-Location (Sea) | 4-Location (hunte) | 5-Location (weser) | 6-Location (Wuemme) | 7-Variable | 8-abv. | 9-abv | 10-Unit | 11-Kennnummer | 12-Prestr | 13-Poststr | 14-VariableLimits | 15-LoadTempData ? y=1
'.dat', '/net/themis/geo/messungen/jadeweser/loc/wasserstand', 'ALW', '', '', '', 'wasserstand', 'wl.', 'wl', 'm NHN', 3, '', ' ', [-4,4], 0;...
'.dat', '/net/themis/geo/messungen/jadeweser/loc/einleitungs-Volumenstrom', '', 'OWH', 'INT', {'HEX'; 'HELL'; 'GRAS'}, 'einleitungs-Volumenstrom', 'sv.', 'sv', 'm NHN', 1058, '', ' seeseitig', [-4,4], 1;...
'.dat', '/net/themis/geo/messungen/jadeweser/loc/Salzgehalt', 'ALW', '', 'HEM', '', 'Salzgehalt', 'sa.', 'sa', 'g/kg', 5, '', ' (2D) seeseitig', [30,35], 1;...
'.dat', '/net/themis/geo/messungen/jadeweser/loc/temperatur', 'ALW', '', 'INT', '', 'temperatur', 'te.', 'te', '°C', 6, '', ' (2D) seeseitig', [0,30], 1 ;...
'.dat', '/net/themis/geo/messungen/jadeweser/loc/truebung', 'DWG', '', 'INT', '', 'truebung', 'tu.', 'tu', 'm', 1161, '', '', [0,5], 0; ... '
};
HYD_YEAR = 2023; % replace 2023 with the actual year you are analyzing
for f = 1:size(INPUT.IN,1)
parameters = INPUT.IN{f,7};
parameters_abv_1 = INPUT.IN{f,8};
parameters_abv_2 = INPUT.IN{f,9};
% Initialize station as empty
station = '';
% Determine the non-empty station for the current parameter
if ~isempty(INPUT.IN{f,3})
station = INPUT.IN{f,3};
elseif ~isempty(INPUT.IN{f,4})
station = INPUT.IN{f,4};
elseif ~isempty(INPUT.IN{f,5})
station = INPUT.IN{f,5};
elseif ~isempty(INPUT.IN{f,6})
station = INPUT.IN{f,6};
end
% If no station is found, continue to the next iteration
if isempty(station)
warning('No station found for parameter %s', parameters);
continue;
end
kennnummers = INPUT.IN{f,11};
PATH_para = INPUT.IN{f,2};
% Assuming 'HYD_YEAR' and 'boe_info' is defined previously in your script
[~, ~, start_end] = boe_info([PATH_para, '/', num2str(HYD_YEAR-1), '/boewrt/'], 'inputfile', [parameters_abv_1, station, '.000.', num2str(HYD_YEAR-1), '.boewrt.dat']);
[temp_stats, temp_year, start_end2] = boe_info([PATH_para, '/', num2str(HYD_YEAR), '/boewrt/'], 'inputfile', [parameters_abv_1, station, '.000.', num2str(HYD_YEAR), '.boewrt.dat']);
start_end = [start_end; start_end2];
[DATA, TIMES] = Year2Hydrolog(PATH_para, station, start_end, HYD_YEAR, kennnummers, parameters_abv_2);
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
  댓글 수: 1
janas
janas 2023년 12월 28일
This works for skipping the empty stations and continue the loop, but I want to extract the data at each of the stations for each parameter

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by