This question is closed. Reopen it to edit or answer.

Getting a for loop to back up through a matrix

1 view (last 30 days)
Eric Metzger
Eric Metzger on 5 Jun 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
Greetings all.
I am haivng trouble getting my script to match up dates from two difference matrixies I have gotten it so it will be move "froward" through them to find the correct dates but this has the untintended consequence of "skipping" dates. After looking at my code and trying a number of nested for and while loops, it is coming down to getting the main for loop to "back up" through the shorter matrix. The for loop uses the shorter matrix to prevent the script from "blowing up" however, I am very hesitant to change the for loop variable to force the script to backup a row or two to get the correct date. Code is below:
precipfile=readtable(precip); %Pull the raw precipitation data
fcstfile=load(fcst); % pull the preprocessed forecast file
control=1; % control functio for the inner while loop to keep it the script from going into an infinate loop
if P<F %Needed to find the shorter file to prevent the function from blowing up.
elseif P>F
for i=1: Fcontrol % outer for loop to process the precipitation data and the forecast data together.
fcstdur=fcstfile(control, 3); % pull the forecast lenght that must be matched (24, 48, or 72 hours)
fcststart=fcstfile(control, 1); % pull the forecast
xkick=0; %File control variable.
while datep~=fcststart
disp(['While Loop. Forecast date: ', num2str(fcststart), ' Preicp file: ', num2str(datep)])
if datep < fcststart
xkick=2; %Control value to move the function along without adjusting the for loop control value directly
elseif fcststart< datep
disp(['Forecast date was, ' num2str(fcststart), ' moved forward to the next line.'])
fcststart=fcstfile(control, 1);
disp(['Main. Forecast date: ', num2str(fcststart), ' Preicp file: ', num2str(datep)])
if xkick==2
disp('Precipitation date is lower than forecast date. For loop advanced.')
This is the main part of the code that controls the progession of the main for loop. The while loop is used to test the dates and ensure they match. If they don't it moves the lower one forward. However, I need a method to move the main for loop back as I have found the loop will skip valid days. This is due to the way the data in the fcstfile is set up. There are times in which there are two instances with the same start date but both need to be verfied and used. This means there are times in which the same date is used twice but the dates will not match and the script will move the main loop forward. As such, I am losing days. One thought was to go to a double nested while loops inside my main for loop and manually control the rows and columns of each matrix to get the needed data. This is tricky though and can bog down the code during execution. Was hoping to avoid that.
I have tried a number of nested for loops but have given up on them as they often create infinite loops.
Note: the "disp" lines are debugging lines to help me find where the script is stuck.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by