Saving a variable in a .mat file

4 ビュー (過去 30 日間)
GCats
GCats 2020 年 8 月 23 日
回答済み: Dinesh Yadav 2020 年 8 月 25 日
Hello everyone,
I have a .m file where a function runs and outputs a series of variables. Right now, I am saving one values out of the if loop in the said function as such:
function [par, ta, xa] = swingup(par)
if strcmp(par.run_type, 'learn')
...
ra = zeros(par.trials, 1);
...
for ii = 1:par.trials
x = swingup_initial_state();
for tt = 1:ceil(par.simtime/par.simstep)
...
r = observe_reward(a, sP, par);
...
% Keep track of cumulative reward
reward = r;
ra(ii) = ra(ii)+reward;
% TODO: check termination condition
if is_terminal(s,par) == 1
break;
end
end
end
save('swingup', 'ra');
In the end, I am interested in saving 'ra' while changing some of the values which 'ra' depends on. So for example changing some values in 'par'. So what I want to achieve is:
  1. Run the code
  2. Save 'ra' as a .m file
  3. Change a variable
  4. Run the code again
  5. Save the new value of 'ra' in a column next to the already existing one
The goal is to have a .mat file with each column the results of ra for different values so that I can plot them. How exactly can I achieve this?
  2 件のコメント
Stephen23
Stephen23 2020 年 8 月 23 日
GCats
GCats 2020 年 8 月 23 日
What I don't understand is how to prevent the new values overwrite the old one when running the same script.

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

回答 (1 件)

Dinesh Yadav
Dinesh Yadav 2020 年 8 月 25 日
Hi,
Have a look at this existing answer for the same question(link). It will resolve your issue.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by