Manipulating a text file
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
james flynn
el 7 de Abr. de 2017
Comentada: Walter Roberson
el 10 de Abr. de 2017
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 comentarios
Respuesta aceptada
Walter Roberson
el 7 de Abr. de 2017
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 comentarios
Walter Roberson
el 10 de Abr. de 2017
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!