Manipulating a text file
1 次查看(过去 30 天)
显示 更早的评论
Hi all. I am trying to replace one of the columns within my text files with a column of zeros. I have been trying to do this by reading each of the columns in the file, replacing the desired column with a matrix of 0's and then writing this to a new file. When running the code the following error is produced which i havent seen before:
Error using horzcat Dimensions of matrices being concatenated are not consistent.
Error in ZlessData (line 13) J = [J1 J2 J3 J4 J5];
Here's the code and data file. Can anyone help?
function ZlessData
Jupiter = dlmread('1959-2019Jupiter.txt');
J1 = Jupiter(:,1);
J2 = Jupiter(:,2);
J3 = Jupiter(:,3);
J5 = Jupiter(:,5);
x = (size(Jupiter,1));
J4(1,x) = 0
J = [J1 J2 J3 J4 J5];
fidJ = fopen('1959-2019JupiterZless','w');
fprintf(fidJ, '%f %f \r\n', [J]');
fclose(fidJ);
0 个评论
采纳的回答
Walter Roberson
2017-4-7
function ZlessData
Jupiter = dlmread('1959-2019Jupiter.txt');
J = Jupiter;
J(:,4) = 0;
fidJ = fopen('1959-2019JupiterZless','w');
fprintf(fidJ, '%f %f \r\n', [J]');
fclose(fidJ);
2 个评论
Walter Roberson
2017-4-10
S = fileread('1959-2019Jupiter.txt');
linelen = find(S==10,1,'first');
Scol = reshape(S, linelen, []).';
Scol(:,18:19) = ' ';
Scol(:,20:23) = '0';
Scol(:,21) = '.';
fidJ = fopen('1959-2019JupiterZless.txt','w');
fwrite(fidJ, Scol.');
fclose(fidJ);
Note: the code in the above form relies upon each line being exactly the same length and the column widths being exactly what your file has.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!