How do I import a txt file with the white spaces

49 visualizaciones (últimos 30 días)
Roshen Jegajeevan
Roshen Jegajeevan el 2 de Mzo. de 2021
Editada: Meg Noah el 2 de Abr. de 2021
I have to edit a .22 file and change one of the columns. It is pretty much a text file with a matrix. It is parsed with a comma as a delimiter. However I want to retrieve the values in between the commas while maintaining the white space between the commas. So when I export the matrix after I've changed it, it will be the exact same with one changed column.
  3 comentarios
Roshen Jegajeevan
Roshen Jegajeevan el 2 de Mzo. de 2021
A = importdata('fort.22');
B = A.textdata;
Z = B{1,6};
X = str2double(Z);
hours = B(:,6);
hoursMat = str2double(hours);
hoursChg = hoursMat - X;
hoursCell = num2cell(hoursChg);
A.textdata(:,6) = hoursCell;
exten = num2cell(A.data);
Final = [A.textdata, exten];
I basically subtracted row 6 by a constant. I used that import function. However it imports it without the white spaces. THe reason I also had to concat A.textdata and exten is bc when I imported using this method, It split it into two different cells in a struct called A.data and A.textdata. Im trying to find a way to import so that I get everything between the comma in the file as one array. For example:
data set looks like this:
OFCL, 27, 192N, 855W, (this format in notepad)
OFCL, 22, 192N, 855W,
OFCL, 21, 192N, 855W,
I want to get it so I can get a cell with each element above between the commas. So the middle would be { 27} and not just {27} (no space before number). Is there any way to do this? It would also allow me to import the array as a whole and not connect the two peices that I got from importData (one struct --> two cells).
Walter Roberson
Walter Roberson el 2 de Mzo. de 2021
A = readlines('fort.22');
Now A{6} would be the 6th line, and you could use text processing on it in any way you like, including extracting from fixed character positions, calculating something, and replacing those fixed character positions using a careful format.
However... when I look at your code, it looks more likely that you want to change column 6, rather than row 6. It does not make a lot of difference though.

Iniciar sesión para comentar.

Respuestas (1)

Meg Noah
Meg Noah el 2 de Abr. de 2021
Editada: Meg Noah el 2 de Abr. de 2021
This seems to work if you don't mind working with a table
readtable('file22.txt','whitespace','','delimiter',',')

Categorías

Más información sobre Data Import and Analysis en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by