- Instead of putting numbers in the variable names use arrays and indexing (easy to access in a loop).
- Putting the code into a function might make that simpler for you: write the code one in the function, test it, and then call it in a loop.
- Remove complex, confusing, and totally superfluous indexing (1:1) for all scalar values.
Loop a script and save each output as a new excel file
1 回表示 (過去 30 日間)
古いコメントを表示
I have this script which only runs on 1 excel file and outputs 1 excel file. I would like to be able to run this script on multiple excel files in the folder and save each as a new output. Example p_1 excel file would save as p1response in a new file and then p_2 excel file would save as p2repsonse. Any suggestions would be helpful!
numData = xlsread('p_2.xlsx');
TP1 = 3000;
TP2 = 6000;
TP3 = 9000;
TP4 = 6000;
TPE1 = numData(1,17);
TPE2 = numData(1,18);
TPE3 = numData(1,19);
TPE4 = numData(1,20);
lookBack = 600;
% AR BUTTONS PRESSES
criticalrowAr = numData(:,3);
% 1 min b4 each thought probe button presses
Quest1onset = TP1(1:1);
Quest1 = (Quest1onset(1:1) - lookBack(1:1));
P1 = criticalrowAr(Quest1:Quest1onset);
bt = find(diff(P1(:,:))>0);
Ar = numel(bt)
Quest2onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1));
Quest2 = (Quest2onset(1:1) - lookBack(1:1));
P2 = criticalrowAr(Quest2:Quest2onset)
bt2 = find(diff(P2(:,:))>0);
Ar2 = numel(bt2)
Quest3onset = (TP1(1:1) + TPE1(1:1)+ TP2(1:1) + TPE2(1:1) + TP3(1:1));
Quest3 =(Quest3onset (1:1)-lookBack(1:1));
P3 = criticalrowAr(Quest3:Quest3onset)
bt3 = find(diff(P3(:,:))>0);
Ar3 = numel(bt3)
Quest4onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1) + TPE2(1:1) + TP3(1:1) + TPE3(1:1) + TP4(1:1));
Quest4 = (Quest4onset(1:1) - lookBack(1:1));
P4 = criticalrowAr(Quest4:Quest4onset)
bt4 = find(diff(P4(:,:))>0);
Ar4 = numel(bt4)
data={Ar,Ar2,Ar3,Ar4} %what to print
col_header={'Thoughtprobe1','Thoughtprobe2','Thoughtprobe3','Thoughtprobe4'}; %Row cell array (for column labels)
xlswrite('My_file.xls',data,'Sheet1','B2'); %Write data
xlswrite('My_file.xls',col_header,'Sheet1','B1'); %Write column header
% BR BUTTONS PRESSES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
criticalrowBr = numData(:,4);
% 1 min b4 each thought probe button presses
Quest1onset = TP1(1:1);
Quest1 = (Quest1onset(1:1) - lookBack(1:1));
P1 = criticalrowBr(Quest1:Quest1onset)
bt = find(diff(P1(:,:))>0);
Br = numel(bt)
Quest2onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1));
Quest2 = (Quest2onset(1:1) - lookBack(1:1));
P2 = criticalrowBr(Quest2:Quest2onset)
bt2 = find(diff(P2(:,:))>0);
Br2 = numel(bt2)
Quest3onset = (TP1(1:1) + TPE1(1:1)+ TP2(1:1) + TPE2(1:1) + TP3(1:1));
Quest3 =(Quest3onset (1:1)-lookBack(1:1));
P3 = criticalrowBr(Quest3:Quest3onset)
bt3 = find(diff(P3(:,:))>0);
Br3 = numel(bt3)
Quest4onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1) + TPE2(1:1) + TP3(1:1) + TPE3(1:1) + TP4(1:1));
Quest4 = (Quest4onset(1:1) - lookBack(1:1));
P4 = criticalrowBr(Quest4:Quest4onset)
bt4 = find(diff(P4(:,:))>0);
Br4 = numel(bt4)
data={Br,Br2,Br3,Br4} %what to print
xlswrite('My_file.xls',data,'Sheet1','B3'); %Write data
% XR BUTTONS PRESSES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
criticalrowXr = numData(:,5);
% 1 min b4 each thought probe button presses
Quest1onset = TP1(1:1);
Quest1 = (Quest1onset(1:1) - lookBack(1:1));
P1 = criticalrowXr(Quest1:Quest1onset)
bt = find(diff(P1(:,:))>0);
Xr = numel(bt)
Quest2onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1));
Quest2 = (Quest2onset(1:1) - lookBack(1:1));
P2 = criticalrowXr(Quest2:Quest2onset)
bt2 = find(diff(P2(:,:))>0);
Xr2 = numel(bt2)
Quest3onset = (TP1(1:1) + TPE1(1:1)+ TP2(1:1) + TPE2(1:1) + TP3(1:1));
Quest3 =(Quest3onset (1:1)-lookBack(1:1));
P3 = criticalrowXr(Quest3:Quest3onset)
bt3 = find(diff(P3(:,:))>0);
Xr3 = numel(bt3)
Quest4onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1) + TPE2(1:1) + TP3(1:1) + TPE3(1:1) + TP4(1:1));
Quest4 = (Quest4onset(1:1) - lookBack(1:1));
P4 = criticalrowXr(Quest4:Quest4onset)
bt4 = find(diff(P4(:,:))>0);
Xr4 = numel(bt4)
data={Xr,Xr2,Xr3,Xr4} %what to print
xlswrite('My_file.xls',data,'Sheet1','B4'); %Write data
% YR BUTTONS PRESSES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
criticalrowYr = numData(:,6);
% 1 min b4 each thought probe button presses
Quest1onset = TP1(1:1);
Quest1 = (Quest1onset(1:1) - lookBack(1:1));
P1 = criticalrowYr(Quest1:Quest1onset)
bt = find(diff(P1(:,:))>0);
Yr = numel(bt)
Quest2onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1));
Quest2 = (Quest2onset(1:1) - lookBack(1:1));
P2 = criticalrowYr(Quest2:Quest2onset)
bt2 = find(diff(P2(:,:))>0);
Yr2 = numel(bt2)
Quest3onset = (TP1(1:1) + TPE1(1:1)+ TP2(1:1) + TPE2(1:1) + TP3(1:1));
Quest3 =(Quest3onset (1:1)-lookBack(1:1));
P3 = criticalrowYr(Quest3:Quest3onset)
bt3 = find(diff(P3(:,:))>0);
Yr3 = numel(bt3)
Quest4onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1) + TPE2(1:1) + TP3(1:1) + TPE3(1:1) + TP4(1:1));
Quest4 = (Quest4onset(1:1) - lookBack(1:1));
P4 = criticalrowYr(Quest4:Quest4onset)
bt4 = find(diff(P4(:,:))>0);
Yr4 = numel(bt4)
data={Yr,Yr2,Yr3,Yr4} %what to print
xlswrite('My_file.xls',data,'Sheet1','B5'); %Write data
% RRR BUTTONS PRESSES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
criticalrowRRr = numData(:,7);
% 1 min b4 each thought probe button presses
Quest1onset = TP1(1:1);
Quest1 = (Quest1onset(1:1) - lookBack(1:1));
P1 = criticalrowRRr(Quest1:Quest1onset)
bt = find(diff(P1(:,:))>0);
RRr = numel(bt)
Quest2onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1));
Quest2 = (Quest2onset(1:1) - lookBack(1:1));
P2 = criticalrowRRr(Quest2:Quest2onset)
bt2 = find(diff(P2(:,:))>0);
RRr2 = numel(bt2)
Quest3onset = (TP1(1:1) + TPE1(1:1)+ TP2(1:1) + TPE2(1:1) + TP3(1:1));
Quest3 =(Quest3onset (1:1)-lookBack(1:1));
P3 = criticalrowRRr(Quest3:Quest3onset)
bt3 = find(diff(P3(:,:))>0);
RRr3 = numel(bt3)
Quest4onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1) + TPE2(1:1) + TP3(1:1) + TPE3(1:1) + TP4(1:1));
Quest4 = (Quest4onset(1:1) - lookBack(1:1));
P4 = criticalrowRRr(Quest4:Quest4onset)
bt4 = find(diff(P4(:,:))>0);
RRr4 = numel(bt4)
data={RRr,RRr2,RRr3,RRr4} %what to print
xlswrite('My_file.xls',data,'Sheet1','B6'); %Write data
% ZRR BUTTONS PRESSES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
criticalrowZRr = numData(:,8);
% 1 min b4 each thought probe button presses
Quest1onset = TP1(1:1);
Quest1 = (Quest1onset(1:1) - lookBack(1:1));
P1 = criticalrowZRr(Quest1:Quest1onset)
bt = find(diff(P1(:,:))>0);
ZRr = numel(bt)
Quest2onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1));
Quest2 = (Quest2onset(1:1) - lookBack(1:1));
P2 = criticalrowZRr(Quest2:Quest2onset)
bt2 = find(diff(P2(:,:))>0);
ZRr2 = numel(bt2)
Quest3onset = (TP1(1:1) + TPE1(1:1)+ TP2(1:1) + TPE2(1:1) + TP3(1:1));
Quest3 =(Quest3onset (1:1)-lookBack(1:1));
P3 = criticalrowZRr(Quest3:Quest3onset)
bt3 = find(diff(P3(:,:))>0);
ZRr3 = numel(bt3)
Quest4onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1) + TPE2(1:1) + TP3(1:1) + TPE3(1:1) + TP4(1:1));
Quest4 = (Quest4onset(1:1) - lookBack(1:1));
P4 = criticalrowZRr(Quest4:Quest4onset)
bt4 = find(diff(P4(:,:))>0);
ZRr4 = numel(bt4)
data={ZRr,ZRr2,ZRr3,ZRr4} %what to print
xlswrite('My_file.xls',data,'Sheet1','B7'); %Write data
% SBR BUTTONS PRESSES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
criticalrowSBr = numData(:,9);
% 1 min b4 each thought probe button presses
Quest1onset = TP1(1:1);
Quest1 = (Quest1onset(1:1) - lookBack(1:1));
P1 = criticalrowSBr(Quest1:Quest1onset)
bt = find(diff(P1(:,:))>0);
SBr = numel(bt)
Quest2onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1));
Quest2 = (Quest2onset(1:1) - lookBack(1:1));
P2 = criticalrowSBr(Quest2:Quest2onset)
bt2 = find(diff(P2(:,:))>0);
SBr2 = numel(bt2)
Quest3onset = (TP1(1:1) + TPE1(1:1)+ TP2(1:1) + TPE2(1:1) + TP3(1:1));
Quest3 =(Quest3onset (1:1)-lookBack(1:1));
P3 = criticalrowSBr(Quest3:Quest3onset)
bt3 = find(diff(P3(:,:))>0);
SBr3 = numel(bt3)
Quest4onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1) + TPE2(1:1) + TP3(1:1) + TPE3(1:1) + TP4(1:1));
Quest4 = (Quest4onset(1:1) - lookBack(1:1));
P4 = criticalrowSBr(Quest4:Quest4onset)
bt4 = find(diff(P4(:,:))>0);
SBr4 = numel(bt4)
data={SBr,SBr2,SBr3,SBr4} %what to print
xlswrite('My_file.xls',data,'Sheet1','B8'); %Write data
% SLR BUTTONS PRESSES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
criticalrowSLr = numData(:,10);
% 1 min b4 each thought probe button presses
Quest1onset = TP1(1:1);
Quest1 = (Quest1onset(1:1) - lookBack(1:1));
P1 = criticalrowSLr(Quest1:Quest1onset)
bt = find(diff(P1(:,:))>0);
SLr = numel(bt)
Quest2onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1));
Quest2 = (Quest2onset(1:1) - lookBack(1:1));
P2 = criticalrowSLr(Quest2:Quest2onset)
bt2 = find(diff(P2(:,:))>0);
SLr2 = numel(bt2)
Quest3onset = (TP1(1:1) + TPE1(1:1)+ TP2(1:1) + TPE2(1:1) + TP3(1:1));
Quest3 =(Quest3onset (1:1)-lookBack(1:1));
P3 = criticalrowSLr(Quest3:Quest3onset)
bt3 = find(diff(P3(:,:))>0);
SLr3 = numel(bt3)
Quest4onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1) + TPE2(1:1) + TP3(1:1) + TPE3(1:1) + TP4(1:1));
Quest4 = (Quest4onset(1:1) - lookBack(1:1));
P4 = criticalrowSLr(Quest4:Quest4onset)
bt4 = find(diff(P4(:,:))>0);
SLr4 = numel(bt4)
data={SLr,SLr2,SLr3,SLr4} %what to print
xlswrite('My_file.xls',data,'Sheet1','B9'); %Write data
% SRR BUTTONS PRESSES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
criticalrowSRr = numData(:,11);
% 1 min b4 each thought probe button presses
Quest1onset = TP1(1:1);
Quest1 = (Quest1onset(1:1) - lookBack(1:1));
P1 = criticalrowSRr(Quest1:Quest1onset)
bt = find(diff(P1(:,:))>0);
SRr = numel(bt)
Quest2onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1));
Quest2 = (Quest2onset(1:1) - lookBack(1:1));
P2 = criticalrowSRr(Quest2:Quest2onset)
bt2 = find(diff(P2(:,:))>0);
SRr2 = numel(bt2)
Quest3onset = (TP1(1:1) + TPE1(1:1)+ TP2(1:1) + TPE2(1:1) + TP3(1:1));
Quest3 =(Quest3onset (1:1)-lookBack(1:1));
P3 = criticalrowSRr(Quest3:Quest3onset)
bt3 = find(diff(P3(:,:))>0);
SRr3 = numel(bt3)
Quest4onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1) + TPE2(1:1) + TP3(1:1) + TPE3(1:1) + TP4(1:1));
Quest4 = (Quest4onset(1:1) - lookBack(1:1));
P4 = criticalrowSRr(Quest4:Quest4onset)
bt4 = find(diff(P4(:,:))>0);
SRr4 = numel(bt4)
data={SRr,SRr2,SRr3,SRr4} %what to print
xlswrite('My_file.xls',data,'Sheet1','B10'); %Write data
% SXR BUTTONS PRESSES%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
criticalrowSXr = numData(:,21);
% 1 min b4 each thought probe button presses
Quest1onset = TP1(1:1);
Quest1 = (Quest1onset(1:1) - lookBack(1:1));
P1 = criticalrowSXr(Quest1:Quest1onset)
bt = find(diff(P1(:,:))>0);
SXr = numel(bt)
Quest2onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1));
Quest2 = (Quest2onset(1:1) - lookBack(1:1));
P2 = criticalrowSXr(Quest2:Quest2onset)
bt2 = find(diff(P2(:,:))>0);
SXr2 = numel(bt2)
Quest3onset = (TP1(1:1) + TPE1(1:1)+ TP2(1:1) + TPE2(1:1) + TP3(1:1));
Quest3 =(Quest3onset (1:1)-lookBack(1:1));
P3 = criticalrowSXr(Quest3:Quest3onset)
bt3 = find(diff(P3(:,:))>0);
SXr3 = numel(bt3)
Quest4onset = (TP1(1:1) + TPE1(1:1) + TP2(1:1) + TPE2(1:1) + TP3(1:1) + TPE3(1:1) + TP4(1:1));
Quest4 = (Quest4onset(1:1) - lookBack(1:1));
P4 = criticalrowSRr(Quest4:Quest4onset)
bt4 = find(diff(P4(:,:))>0);
SXr4 = numel(bt4)
data={SXr,SXr2,SXr3,SXr4} %what to print
xlswrite('My_file.xls',data,'Sheet1','B11'); %Write data
filedata = xlsread('My_file.xls')
sum1 = filedata(:,1)
sum1c= sum(sum1)
sum2 = filedata(:,2)
sum2c= sum(sum2)
sum3 = filedata(:,3)
sum3c= sum(sum3)
sum4 = filedata(:,4)
sum4c= sum(sum4)
data={sum1c,sum2c,sum3c,sum4c} %what to print
xlswrite('My_file.xls',data,'Sheet1','B12'); %Write data
1 件のコメント
Stephen23
2018 年 2 月 27 日
Simple: use a loop.
採用された回答
Rik
2018 年 2 月 27 日
Use a loop.
Replace all occurrences of "xlswrite('My_file.xls'" with "xlswrite(outputfilename"
ext='.xlsx';
xl_file_list=dir(['p_*' ext]);
xl_file_list={xl_file_list(:).name};
xl_response_list=cellfun(@(x) ['p' x(3:(end-length(ext))) 'response' ext],...
xl_file_list,'uniformoutput',false);
for file_number=1:length(xl_file_list)
outputfilename=xl_response_list{file_number};
numData = xlsread(xl_file_list{file_number});
%put original code here (excluding the first line)
end
その他の回答 (1 件)
Bob Thompson
2018 年 2 月 27 日
[filename, filedir] = uigetfile('*.txt','MultiSelect','on'); % UI to select isothermal files
filepath = fullfile(filedir,filename);
filepath = filepath'; % Transpose from columns to rows
filein = struct('name',filepath); % Enter into structured array to be read by 'fopen'
num = length(filein);
for i = 1:num; % Loop through all input files
data = textread(filein(i).name,'','headerlines',1,'delimiter',','); % Read text data from isothermal file.
This is a bit of code I like to use for what you're talking about. I like uigetfile() because then I can pick the set of files I want and it will automatically generate the pathing to that location. Organizing it into a structure isn't really necessary, but I haven't had any problems with it, except in older versions of matlab (2007a).
Your xlsread code would replace the textread command at the end, and the rest of your code would follow inside of the for loop. At the end of your code if you keep the xlswrite command inside the for loop than it will write an individual file for each loop, just be sure and set a command to change the out file name.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Import from MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!