Manipulating a text file

조회 수: 1 (최근 30일)
james flynn
james flynn 2017년 4월 7일
댓글: Walter Roberson 2017년 4월 10일
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);

채택된 답변

Walter Roberson
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
james flynn
james flynn 2017년 4월 10일
Thank you. However, it still changes the layout of the document into what seems to be like a continuous string of data. I require the document to have the data layed out in rows like this, as it is in the original document:
1993 1 0.983 0.00 100.58 -3.04 264.11 24.73
1993 2 0.983 0.00 101.60 -3.16 251.00 25.75
1993 3 0.983 0.00 102.62 -3.27 237.82 26.76
1993 4 0.983 0.00 103.64 -3.39 224.65 27.78
1993 5 0.983 0.00 104.66 -3.50 211.48 28.79
Walter Roberson
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by