Problem with a for loop

52 views (last 30 days)
Manny
Manny on 12 Dec 2024 at 6:13
Commented: Manny on 12 Dec 2024 at 15:44
Hi
I am new to MATLAB. I have a for loop that produces values for the first iteration no problem. However, when it starts the second iteration of the loop then the code fails:
% LOAD TABLE FROM SQL SERVER
CIV = sqlread(ConnectionString,'CIV');
SymbolList = sqlread(ConnectionString,'SymbolList');
for m = 1:height(SymbolList)
CIV = CIV(strcmp(CIV.UNDERLYING_SYMBOL,table2array(SymbolList(m,"SYMBOL"))),:);
do more stuff
end
There are 3 test values in SymbolList so the above for loop should run for 3 times. I checked SymbolList and there are 3 values. Suppose the symbols are ABC, DEF, GHI. When the first iteration is done it is using ABC values from CIV. The second iteration is applying DEF filter to CIV. However, this is guranateed to produce an empty set because CIV only contains ABC. The only solution I can think of is re-pull data from the source (SQL Server) but that might not be efficient. How can I adjust the above for loop so it will work correctly without re-reading the SQL Server data?
Thank you
  4 Comments
Steven Lord
Steven Lord on 12 Dec 2024 at 15:42
What does "fails" mean in this context?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
Note that whatever CIV contained before the line of code in the for loop body ran is not what it'll contain after. Are you certain you want to overwrite that variable, or do you want to extract a subset of CIV into a temporary variable so your next loop iteration operates on the original CIV?
Manny
Manny on 12 Dec 2024 at 15:44
I figured out the problem and fixed it. the code is now working as expected. thank you

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!