How to read a specially structured data file
36 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jake
el 27 de Feb. de 2023
I have a *.5P file (file is an output from the software WAMIT, and can be read by Matlab and/or any text editor) that I want to read through Matlab. Since I cannot upload a *.5P file here in the forum, I changed it to a *.txt file and attached the sample file here.
Now, one line of this data file would include 19 columns (each separated by a tab or space). However, the specific structure of the output file "wraps" each data line to include only 15 columns, and the next 4 lines go into a new line. I'm trying the following code to read the data.
fid = fopen('test.5p');
C = cell2mat(textscan(fid, '%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f'));
fclose(fid);
But this doesn't give me the structure I want, and instead, I get something similar to the following.
How can I read these wrapped lines to be one single line?
TIA!
** EDIT: I realized that I made a mistake with the question and the actual structure of the data file is a bit more complex than what I thought and had attached here. I posted a separate question detailing that.
0 comentarios
Respuesta aceptada
Stephen23
el 27 de Feb. de 2023
fid = fopen('test.txt','rt');
mat = fscanf(fid,'%f',[19,Inf]).';
fclose(fid);
display(mat)
Más respuestas (2)
Askic V
el 27 de Feb. de 2023
Editada: Askic V
el 27 de Feb. de 2023
I would also like to suggest my naive and inefficient approach:
A = dlmread('testfile5p.txt');
nr_rows = size(A,1);
A2 = zeros(ceil(size(A,1)/2),19);
for i = 1:nr_rows-1
A2(i,:) = [A(i,:), A(i+1,1:4)];
end
B = A2(1:2:end,:);
The solution with using Inf plus transponse is really mind blowing. I would never expect that.
0 comentarios
Ver también
Categorías
Más información sobre Text Files 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!