How to replace columns in a datafile after performing mathematical operations to them?
5 views (last 30 days)
Show older comments
I have a datafile having 40 columns and have to divide first column with a constant number (let say 5) and rest 39 columns with a different number (i.e. 7). So, I want to replace all the columns in a new datafile. Can you suggest any short method for it.
I am trying the following code but its not replacing the column, and just appending a column according to last given mathematical operation (i.e. here appending the new calculated 3rd column to the first column).
x=5; % a constant
y=7; %another constant
a = textread('file1.txt');
a1 = a(:,1)/x;
a2 = a(:,2)/y;
a3 = a(:,3)/y; %third column
save ('New_file1.txt','-ascii')
type('New_file1.txt')
0 Comments
Accepted Answer
Voss
on 28 May 2022
x=5; % a constant
y=7; %another constant
a = readmatrix('file1.txt');
disp(a)
new_a = a;
new_a(:,1) = new_a(:,1)/x;
new_a(:,2:end) = new_a(:,2:end)/y;
disp(new_a)
writematrix(new_a,'New_file1.txt')
type('New_file1.txt')
6 Comments
More Answers (0)
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!