How to log the data in the workspace without modifying the content of the for loop?

42 次查看(过去 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.

回答(3 个)

Matt J
Matt J 2024-9-12,1:00
编辑:Matt J 2024-9-12,1: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
Good to go Good to go Good to go Good to go Good to go
disp(Matrix)
110 210 310 120 220 320 130 230 330 140 240 340 150 250 350
  1 个评论
Andy
Andy 2024-9-12,15:46
Please check my Edit 1 in the post regarding to my "store into data". You might be right, LOOP_B does not have to be a loop. The problem is that LOOP_A is a script with for loop and keeps overwriting the workspace variable. More importantly, I cannot do too much to LOOP_A. I'm not sure how I can store each variable value into an array before it gets overwrite and without editing the content of LOOP_A. If I can edit LOOP_A I would just store all of those variables into an array before they got overwriten...

请先登录,再进行评论。


Catalytic
Catalytic 2024-9-12,1:20
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
Good to go Good to go Good to go Good to go Good to go
storedData = 5x2
10 100 20 200 30 300 40 400 50 500
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
  1 个评论
Andy
Andy 2024-9-12,15:32
编辑:Andy 2024-9-12,15:47
Please check my Edit 1 in the post regarding to "store into data". You might be right, LOOP_B does not have to be a loop. The problem is that LOOP_A is a script with for loop and I cannot edit too much of LOOP_A.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2024-9-12,1:24
Your structure reminds me of using SPMD together with spmdSend and spmdBarrier (formerly labSend and labBarrier)
But your structure also reminds me of using parfeval and afterEach
  1 个评论
Andy
Andy 2024-9-12,17:10
I'm not familiar with either one. I looked into parfeval and afterEach but I don't think I can use it since the variables in LOOP_A has a lot of complicated computation involving previous value in the past iteration(s). SPMD seems closer to what I'm looking for. Maybe I can try spmdBarrier in LOOP_A and done by one worker, then make another worker handle storing the data into array and use spmdBarrier too. Thanks!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by