多次运行Matlab脚本

时间:2016-11-01 01:56:24

标签: matlab

所以我有一个matlab.m文件脚本。文件运行时。它生成一个向量。我想保存该向量并重新运行该脚本。如何在整个脚本文件上放置一个循环并创建一个vector_ {i},其中索引输入文件的名称?我会发布代码,但如果没有我桌面上的数据,它将无法工作。

[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

2 个答案:

答案 0 :(得分:2)

使脚本成为一个功能。然后在循环中执行该函数多少次。例如:

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

稍后,只需在函数上运行for循环并将结果存储到单元格数组中:

for k = 1:10 
    A{k} = my_function(k) 
end

答案 1 :(得分:-1)

使脚本成为一个功能。然后在循环中执行该函数多少次。例如:

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

稍后,只需在函数上运行for循环。

for k = 1:10 
    assignin('base', ['A_', num2str(k)], my_function(k)) 
end