How to update workspace variable content for every iteration?

9 ビュー (過去 30 日間)
akshay raj
akshay raj 2015 年 1 月 19 日
コメント済み: akshay raj 2015 年 1 月 21 日
Hi,
So I have this code
h = SomeFunction;
h.run
for i = 1:4
data_local = h.data;
data_local = data_local+rand(1)
plot(data_local);
pause(2);
end
which outputs 128x25 matrix every second(with a time gap of 2 secounds)
so for each iteration I want this to update the variable in the work space so i used
assignin('base','data',data_local)
inside the loop.
this seems to work, but the thing is only the last value is being stored (after the loop is completed).
Can anyone tell me how to update the variable in the workspace on each loop?
thanks in advance.
  2 件のコメント
Stephen23
Stephen23 2015 年 1 月 19 日
Are you running this code from a script or in the MATLAB command window?
Stephen23
Stephen23 2015 年 1 月 19 日
Do not use the variable names i or j for the names of loop variables, as these are both the names of the inbuilt imaginary unit .

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2015 年 1 月 19 日
編集済み: Stephen23 2015 年 1 月 19 日
You did not state in your question how you are runing this code, so I assumed that it is in a script. You can store the output of each iteration in an array, possibly a cell array , like in this example:
h = SomeFunction;
h.run
out = cell(1,4);
for k = 1:4
out{k} = h.data + rand(1);
plot(out{k});
pause(2);
end
You will find all of the data in the cell array out. Cell arrays use their own special indexing .
Another method would be to store it in a simple numeric array, which might make some later processing much easier as you could use vectorized operations on the complete array:
h = SomeFunction;
h.run
out = nan([size(A.data),4]);
for k = 1:4
out(:,:,k) = h.data + rand(1);
plot(out(:,:,k));
pause(2);
end
Also I changed the name of your loop variable to k. Do not use the variable names i or j for the names of loop variables, as these are both the names of the inbuilt imaginary unit .
  3 件のコメント
Stephen23
Stephen23 2015 年 1 月 20 日
編集済み: Stephen23 2015 年 1 月 20 日
If you run this as a script then the variables must be in the base workspace, as this is the one and only workspace that scripts use. What do you mean "not able to see the the variable content in the workspace" ? Can you see the variables in the workspace? Is your workspace panel open (or docked, or undocked and hiding..)? Did you try double-clicking on the variable in your workspace?
Please run the script, and then upload a screenshot of your MATLAB window.
akshay raj
akshay raj 2015 年 1 月 21 日
sorry this is actually a function, and I think i just need to put the out variable inside
function [out] = fileName
thank you soooooo... much for your help. this works.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by