0

So I have a matlab.m file script. When the file runs. It generates a vector. I want to save that vector and rerun the script all over again. How do I put a loop on the entire script file and create a vector_{i}, where the index enters the name of the file? I would post the code but it wont work without the data on my desktop.

[data,labels]=xlsread('C:\Users\Hilbert\Desktop\matlab\matlabdata_abe.xlsx');


gdp=log(data(:,1)./lagmatrix(data(:,1),1)) %GDP
ip=log(data(:,2)./lagmatrix(data(:,2),1)) %IP
tnx=data(:,3) %TNX
m2=log(data(:,4)./lagmatrix(data(:,4),1)) %M2
cpi=log(data(:,5)./lagmatrix(data(:,5),1)) %CPI
ffed=log(data(:,6)./lagmatrix(data(:,6),1)) %FedFund


Dgdp=gdp
inflation=cpi
Dm2=m2
ffr_=ffed


data=[Dgdp(54:length(cpi)), inflation(54:length(cpi)), Dm2(54:length(cpi)), ffr_(54:length(cpi)) ]; 

data_L1=lagmatrix(data,1)
data_L2=lagmatrix(data,2)
data_L3=lagmatrix(data,3)
data_L4=lagmatrix(data,4)
mat=[ones(1,size(data_L1',2));data_L1';data_L2';data_L3';data_L4']
mat=mat(:,5:end)

X=[data';data_L1';data_L2';data_L3']
X=X(:,5:end)


mat=mat';
X=X'
Fhat=(inv(mat'*mat) * mat'*X)';

nobs=size(data,1)
p=4

yhat= mat*Fhat'
yhat=yhat(:,1:4)
data_sample=data(5:nobs,:)
res=data_sample - yhat

res_{loopindexnumber}=res  %saves the vector and re-runs the entire cost again the idea is to bootstrap the data by running many simulations and saving the residual vector
Cœur
  • 37,241
  • 25
  • 195
  • 267
jessica
  • 1,325
  • 2
  • 21
  • 35

2 Answers2

2

Make the script a function. And then execute the function in a loop how many times you want. For example:

function res = my_function(k)
  % your script goes here.
  % the function is saved in my_function.m file
  % some calucations producing return_vector using k parmeter

  res = return_vector

Later on, just run a for loop over the function and store the results to a cell array:

for k = 1:10 
    A{k} = my_function(k) 
end
sco1
  • 12,154
  • 5
  • 26
  • 48
-1

Make the script a function. And then execute the function in a loop how many times you want. For example:

function res = my_function(k)
  % your script goes here.
  % the function is saved in my_function.m file
  % some calucations producing return_vector using k parmeter

  res = return_vector

Later on, just run a for loop over the function.

for k = 1:10 
    assignin('base', ['A_', num2str(k)], my_function(k)) 
end
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • hey marcin. When I run the for loop on the function, I use your – jessica Nov 01 '16 at 03:03
  • 1
    @jessica if you want different vectors, you can return them from the my_function, or parametrise it, so that it saves the vectors based on loop index. – Marcin Nov 01 '16 at 03:03
  • for k = 1:10 assignin('base', ['A_', num2str(k)], my_function(k)) end – jessica Nov 01 '16 at 03:04
  • 3
    I would highly recommend not teaching people to do this. It may work for the immediate problem but [later addressing](http://stackoverflow.com/questions/28592021) these dynamically named variables quickly [becomes a nightmare](http://stackoverflow.com/questions/40226214/) that [just gets worse](http://stackoverflow.com/questions/40245465/). These are not isolated examples. If you dynamically assign variables then you will end up later having to dynamically addressing them, which more often than not leads to poorly functioning, difficult to maintain code. – sco1 Nov 01 '16 at 16:54
  • @excaza If you checked my earlier comments, you would see I recommeded against polluting global namespace, and use map or somemething else. But nevertheless my anwser is correct to OP question, and SO is about solving problems, not about telling ppl that their problem is bad and not giving them correct anwsers to their immediate problems. There is no other solution to OP problem, unless you assume you know better what OP's real situation is. – Marcin Nov 01 '16 at 22:56
  • There are plenty of other solutions that are far better than randomly spawning variables in other workspaces. Cell arrays, multidimensional arrays, dynamic structure indexing, map containers, etc. Really anything but `eval`, `assignin`, or `evalin`. It's poor programming and giving people poor code to copy/paste just compounds the issue. There's no reason you can't include proper approaches alongside the "immediate problem." But hey, it's all about that rep... – sco1 Nov 02 '16 at 04:27
  • @excaza So provide one. Note this is follow-up to [this](http://stackoverflow.com/questions/40353328/loop-string-in-variable-name-matlab). So make a new anwser which agrees with the former question of the OP. – Marcin Nov 02 '16 at 04:29
  • Ok, I have added an answer – sco1 Nov 02 '16 at 04:33