How to log the data in the workspace without modifying the content of the for loop?
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am currently using MATLAB 2022a.
I have a for loop (let's call this for loop LOOP_A) doing some calculation and each calculation is depending on some values from the previous loop.
Without editting too much of the content of LOOP_A, I want to log those variables at the end of each loop.
I'm thinking about having another for loop (let's call this one LOOP_B) that log the data in the workspace for each loop of LOOP_A till the entire execution of the LOOP_A is finished.
Here is what I would like to have:
% Repeat the following till all loops are finished:
% LOOP_A:
% Pause after one iteration
% Send data from workspace to LOOP_B
% Wait till LOOP_B send "good to go" signal if not finished
% LOOP_B:
% Receive data from LOOP_A
% Store the data
% Send "good to go" signal to LOOP_A
% LOOP_A:
% Continue running for one iteration
% Send data from workspace to LOOP_B
% Wait till LOOP_B send "good to go" signal if not finished
% ...
Is there a way I can do that?
Edit 1:
To be more specific, I'm storing the data into another timeseries data. If LOOP_A has 100 loops, I would like to store them into an array of 100 elements and give it a time.
0 commentaires
Réponses (3)
Matt J
le 12 Sep 2024
Modifié(e) : Matt J
le 12 Sep 2024
What does "log the data" mean? Do you mean you want to store it to a file on disk, or do you want to store it in some sort of array? And in what way is loop B a loop at all? It doesn't seem to be doing any repetitions of its own.
In any case, loops don't have their own private workspaces. They exist in a common workspace with the code that surrounds them. For example, in the nested loop below, the inner loop has access not only to the variables B and D, but automatically also to A and C in the loop outside of it.
for A=1:5
C=10*A;
for B=1:3
D=100*B;
Matrix(A,B) = C+D;
end
disp 'Good to go'
end
disp(Matrix)
Catalytic
le 12 Sep 2024
I also do not see how B is a loop if it doesn't have it's own iterations. But anyway, maybe this is what you meant -
storedData = runTask
function storedData =runTask
storedData=nan(5,2);
for A=1:5
Adata=10*A;
nestedLogger() %i.e., "Loop B"
end
function nestedLogger()
Bdata=100*A;
storedData(A,:) = [Adata,Bdata];
disp 'Good to go'
end
end
Walter Roberson
le 12 Sep 2024
Your structure reminds me of using SPMD together with spmdSend and spmdBarrier (formerly labSend and labBarrier)
Voir également
Catégories
En savoir plus sur Matrix Indexing 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!