フィルターのクリア

adding column in text file with value

1 回表示 (過去 30 日間)
Lalit Patil
Lalit Patil 2012 年 11 月 27 日
Suppose i have a text file that contains 1 column, 68160 rows of random data.
Now, i want
for first 480 rows add 0 in first column,
next 480 rows add 5,
next 480 rows add 10,
next 480 rows add 15,
and continue this with increment of 5, until all columns get finished.. and store all this new data in second column...
I want to write MATLAB code.. So, how to do it...?

採用された回答

Muruganandham Subramanian
Muruganandham Subramanian 2012 年 11 月 27 日
編集済み: Muruganandham Subramanian 2012 年 11 月 27 日
Check the below code: a=zeros(68160,1);
k=480;
l=5;
j=1;
for i=1:numel(a)
if i<=k
a(i)=a(i)+0;
else
if i <= (j+1)*k
a(i)=a(i)+j*l;
else
j=j+1;
a(i)=a(i)+j*l;
end
end
end
  1 件のコメント
Lalit Patil
Lalit Patil 2012 年 11 月 27 日
Thank you... It get solved..

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2012 年 11 月 27 日
At first you have to read the file. Therefore you have to know the format of the contents: "random data" could be nearly everything, e.g. floating point numbers like 0.1098435, integers like 134, strings like 'ajdtaoieurgkjahsvbdk', etc. "Adding 0" might imply that you are talking about integer values.
Then a loop seems to be the easiest solution. Just try it and return to the forum in case of problems. It is much easier for the forum users to suggest improvements than to write the complete program from scratch.
  1 件のコメント
Lalit Patil
Lalit Patil 2012 年 11 月 27 日
I got it solved...!
clear all;
clc;
fid = fopen('data.txt','wt');
for j=1:500
fprintf(fid,'%f\n',j);
end
fclose(fid);
fileID = fopen('data.txt');
C = textscan(fileID, '%f32');
fclose(fileID);
x=cell2mat(C(:,1));
for m = 0:49
for k = (1+(10*m)):(10+(10*m))
y(k) = x(k) + (0+(5*m));
end
end
y = y';
fid = fopen('data.txt','wt');
for j=1:500
fprintf(fid,'%f %f\n',j,y(j));
end
fclose(fid);
If it is possible to short this code then suggest me...

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by