Running script for different datafiles consecutively

1 次查看(过去 30 天)
My script is:
clc;
clear;
% Import data
% PD: probability of default
% CM: covariance matrix
% DT: default threshold
PD = xlsread('Book1.xlsx','PD');
CM = xlsread('Book1.xlsx','COV');
DT = xlsread('Book1.xlsx','DT');
Original_PD = PD; %Store original PD
LM_rows = size(PD,1)+1; %Expected LM rows
LM_columns = size(PD,2); %Expected LM columns
LM_FINAL = zeros(LM_rows,LM_columns); %Dimensions of LM_FINAL
for i = 1:size(PD,2)
PD = Original_PD(:,i);
options = optimset('Display','iter');
x0 = zeros(size(PD,1)+1,1);
[LM,fval,exitflag] = fsolve(@(x)ConstLM(x,PD,CM,DT), x0, options);
LM_FINAL(:,i) = LM;
end
Now I have many datafiles, namely:
Book1.xlsx
Book2.xlsx
...
Book100.xlsx
Currently I am running the script manually for each Book, how can I run let it run automatically for all 100 Book's?
The final result I am after is the LM_FINAL variable which is a 3x1348 matrix of values. When automated for 100 Book's, I will end up with 100 sets of 3x1348 matrix of values, ie, 300x1348 matrix of values. I would like to display this entire 300x1348 matrix of values in one variable called LM_FINAL_ALL.
Lastly, if required, an example of my datafile Book1 (excel file) can be found here: https://www.dropbox.com/s/bf16cpi6532ze1j/Book1.xlsx
Many thanks. If I am unclear please let me know and I will try explain further.

采纳的回答

Mathurin
Mathurin 2013-9-4
编辑:Mathurin 2013-9-4
clear;
options = optimset('Display','iter');
for i=1:100
dataFile = ['Book' num2str(i) '.xlsx'];
PD = xlsread(dataFile,'PD');
CM = xlsread(dataFile,'COV');
DT = xlsread(dataFile,'DT');
Original_PD = PD; %Store original PD
LM_rows = size(PD,1)+1; %Expected LM rows
LM_columns = size(PD,2); %Expected LM columns
LM_FINAL = zeros(LM_rows,LM_columns); %Dimensions of LM_FINAL
for j = 1:size(PD,2)
PD = Original_PD(:,j);
x0 = zeros(size(PD,1)+1,1);
[LM_FINAL(:,j),fval,exitflag] = fsolve(@(x)ConstLM(x,PD,CM,DT), x0, options);
end
LM_FINAL_All{i} = LM_FINAL;
end
  1 个评论
sittmo
sittmo 2013-9-5
Hi Mathurin, thank you, this is almost what I was looking for. However, the variable LM_FINAL_All contains 1x10 cells, but I have to click on each cell to view the actual values. Is there a way to not have cells but rather 300x1348 of values?

请先登录,再进行评论。

更多回答(3 个)

Alan Weiss
Alan Weiss 2013-9-3
One more bit of advice: don't use optimset in the loop. Initialize your options before running the loop, calling optimset just once. This could save you a bit of time.
Alan Weiss
MATLAB mathematical toolbox documentation

Ilham Hardy
Ilham Hardy 2013-9-3
  • clear clc and clear part from your script
  • You need another for loop encapsulating your current script
  • Use fprintf to dynamically change your excel file names inside the script
  • Use cell for LM_FINAL_ALL parameter
That's bassically it.
  1 个评论
sittmo
sittmo 2013-9-3
Thanks for the hints llham, however I am extremely new to MATLAB and still trying to learn the basic coding structure. I know I need to create an outer loop that loops through each of my datafiles but I am not sure how to do so (such as using fprintf), could you please show me? Thanks.

请先登录,再进行评论。


Mathurin
Mathurin 2013-9-4
clear; options = optimset('Display','iter'); for i=1:100 dataFile = ['Book' num2str(i) '.xlsx']; PD = xlsread(dataFile,'PD'); CM = xlsread(dataFile,'COV'); DT = xlsread(dataFile,'DT'); Original_PD = PD; %Store original PD LM_rows = size(PD,1)+1; %Expected LM rows LM_columns = size(PD,2); %Expected LM columns LM_FINAL = zeros(LM_rows,LM_columns); %Dimensions of LM_FINAL for j = 1:size(PD,2) PD = Original_PD(:,j); x0 = zeros(size(PD,1)+1,1); [LM_FINAL(:,j),fval,exitflag] = fsolve(@(x)ConstLM(x,PD,CM,DT), x0, options); end LM_FINAL_All{i} = LM_FINAL; end

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by