필터 지우기
필터 지우기

How to run the same script but for different datafiles consecutively

조회 수: 3 (최근 30일)
So I have the following script:
clc;
clear;
LM = xlsread('LagrangeMultiplier.xlsx','LM');
CM = xlsread('LagrangeMultiplier.xlsx','CM');
DT = xlsread('LagrangeMultiplier.xlsx','DT');
for i = 1:size(LM,2)
JPD(i,:) = exp(-1-sum(LM(:,i)))*mvncdf(DT, Inf*ones(size(DT,1),1),0,CM);
end
Basically I have datafiles named:
LagrangeMultiplier1.xlsx
LagrangeMultiplier2.xlsx
LagrangeMultiplier3.xlsx
and so on until LagrangeMultiplier100.xlsx
Currently I'm running this script manually for each file, ie, I have LagrangeMultiplier1.xlsx as the name in the script, then I copy the values from the JPD workspace into excel, then put LagrangeMultiplier2.xlsx as the datafile name and so on. How can I automate this so that MATLAB automatically runs the script for all 100 datafiles and stores all 100 arrays for JPD into the workspace?
Thanks

채택된 답변

Geert
Geert 2013년 8월 30일
편집: Geert 2013년 8월 30일
@ Azzi: It is indeed not recommended to create severable variables by the
eval([ 'JPD',num2str(ii),' = JPD;'])
command. I thought Simon wanted to do it anyway, so hence I delivered ;)
@ Simon: From your screenshot it looks like your data is one-dimensional. You can hence change the code as follows to obtain what you want:
nb_files = 100;
% preallocate space for JPD
LM = xlsread('LagrangeMultiplier1.xlsx','LM');
JPD = zeros(length(LM),nb_files);
% read in all the files and store them as columns in JPD variable
for kk=1:nb_files
file=sprintf('LagrangeMultiplier%d.xlsx',kk);
LM = xlsread(file,'LM');
CM = xlsread(file,'CM');
DT = xlsread(file,'DT');
for ii = 1:length(LM)
JPD(ii,kk) = exp(-1-sum(LM(:,ii)))*mvncdf(DT, Inf*ones(size(DT,1),1),0,CM);
end
end

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 29일
for k=1:100
file=sprintf('LagrangeMultiplier%d.xlsx',k);
LM = xlsread(file,'LM');
CM = xlsread(file,'CM');
DT = xlsread(file,'DT');
for i = 1:size(LM,2)
JPD(i,:) = exp(-1-sum(LM(:,i)))*mvncdf(DT, Inf*ones(size(DT,1),1),0,CM);
end
out{k}=JPD
end
  댓글 수: 3
Ilham Hardy
Ilham Hardy 2013년 8월 30일
change
out{k}=JPD
into
JPDall{k}=JPD
to accomodates your need..
sittmo
sittmo 2013년 8월 30일
편집: sittmo 2013년 8월 30일
Thanks, however it still doesn't do what I intended, the result I get is shown in this screenshot: http://imageshack.us/photo/my-images/832/umzs.jpg/
However as you can see, I need to click on JPDall{1,1} to see the 1348x1 column of values. Since I want to copy each cell of values into excel, I would need to open each of the 9 JPDall cells individually and copy each into excel, that would take quite a long time when I increase it to 100 cells. Is there a way to output all JPDall cells into one variable, so that I have 1348x9 matrix of values so I can copy it all at once into excel.

댓글을 달려면 로그인하십시오.


Geert
Geert 2013년 8월 29일
편집: Geert 2013년 8월 29일
The following code will save your JPD variable for LagrangeMultiplier1.xlsx as JPD1 in the workspace, the JPD variable for LagrangeMultiplier2.xlsx as JPD2, and so on...
clc;
clear;
nb_files = 100;
for ii = 1:nb_files
filename = ['LagrangeMultiplier',num2str(ii),'.xlsx'];
LM = xlsread(filename,'LM');
CM = xlsread(filename,'CM');
DT = xlsread(filename,'DT');
JPD = [];
for jj = 1:size(LM,2)
JPD(jj,:) = exp(-1-sum(LM(:,jj)))*mvncdf(DT, Inf*ones(size(DT,1),1),0,CM);
end
eval([ 'JPD',num2str(ii),' = JPD;'])
end
  댓글 수: 4
Ilham Hardy
Ilham Hardy 2013년 8월 30일
I think what he meant is just like Azzi's answer.. by putting it in one cell.
sittmo
sittmo 2013년 8월 30일
What I mean is, have JPD1, JPD2... all within one variable called JPDall or something, because currently I have to open JPD1 to see the values in JPD1, then open JPD2 to see the values in JPD2... what I was hoping is to have all those values under one variable JPDall so that when I open JPDall I see the values for all JPD1, JPD2 ...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by