loop txt files and save files

I do not have much knowledge in Matlab, but want to do the following, and need help.
I have two excel files, each has five columns of data. I need to add each column data in these two files with varying percentages in a loop, and save the new data in a series of files with the percentage in the files' names. Example:
%%%%%%%input data1%%%%%%%%%%%%%%%
data1=importdata('rawdata1.txt');
f1=data1(:,1);
e11=data1(:,2);
e12=data1(:,3);
u11=data1(:,4);
u12=data1(:,5);
%%%%%%%input data2%%%%%%%%%%%%%%%
data2=importdata('rawdata2.txt');
f2=data2(:,1);
e21=data2(:,2);
e22=data2(:,3);
u21=data2(:,4);
u22=data2(:,5);
%%%%%%%%%%%%%%%composite data%%%%%%%%%%%%
for x=0:0.1:1
data="Data"+x
f=x*f1+(1-x)*f2;
e1=x*e11+(1-x)*e21;
e2=x*e12+(1-x)*e22;
u1=x*u11+(1-x)*u21;
u2=x*u12+(1-x)*u22;
%%%%%%%%%save in data
data(:,1)=f;
data(:,2)=e1;
data(:,3)=e2;
data(:,4)=u1;
data(:,5)=u2;
xlswrite('data',data);
end;

9 Comments

You do not need 5 different variables for that. You could use
d1 = data1(:,1:5);
d2 = data2(:,1:5);
for x = 0:0.1:1
data = d1 .* x + d2 .* (1-x);
filename = sprintf('data%2.1f.xls', x);
xslwrite(filename, data);
end
I tried, only got one file with the name of "filename"...
What is the part 'data%2.1f.xls'? What is "%2.1f"?
addy fang
addy fang on 10 Jul 2020
Edited: addy fang on 10 Jul 2020
got the meaning of the symbol, but I tried, only got one file with the name of "filename"...not filename of "datax", and I expect to have 11 files with filenames of data0, data0.1, data0.2....
>> for x = 0:0.1:1
filename = sprintf('data%2.1f.xls', x)
end
filename =
'data0.0.xls'
filename =
'data0.1.xls'
filename =
'data0.2.xls'
filename =
'data0.3.xls'
filename =
'data0.4.xls'
filename =
'data0.5.xls'
filename =
'data0.6.xls'
filename =
'data0.7.xls'
filename =
'data0.8.xls'
filename =
'data0.9.xls'
filename =
'data1.0.xls'
Notice that what I posted is
xslwrite(filename, data);
What you did instead is
xslwrite('filename', data);
addy fang
addy fang on 10 Jul 2020
Edited: addy fang on 10 Jul 2020
Thank Walter and Madhan a lot for the great help!

Sign in to comment.

Answers (0)

Categories

Asked:

on 10 Jul 2020

Edited:

on 10 Jul 2020

Community Treasure Hunt

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

Start Hunting!