How to log the data in the workspace without modifying the content of the for loop?
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(3 个)
Matt J
2024-9-12
编辑:Matt J
2024-9-12
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
2024-9-12
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
2024-9-12
Your structure reminds me of using SPMD together with spmdSend and spmdBarrier (formerly labSend and labBarrier)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!