How to read .XY file?

96 visualizaciones (últimos 30 días)
Mateusz Kiersnowski
Mateusz Kiersnowski el 26 de Jul. de 2022
Comentada: Mateusz Kiersnowski el 27 de Jul. de 2022
i've got a file with ext .xy that i want to read, cant attached it here as it is not supported here. it looks liek that:
7004825,RMS:30.78,No:1082.
20.00000000,2.60350425E-02
25.00000000,4.05571833E-02
30.00000000,5.82764596E-02
35.00000000,7.92009458E-02
40.00000000,1.03343524E-01
45.00000000,1.30721480E-01
50.00000000,1.61356464E-01
55.00000000,1.61643982E-01
60.00000000,1.61960006E-01
65.00000000,1.62304759E-01
after that there is a one empty line and it repeats with diffrent numbers.
i want to read 1st line with info and then read 2 columns.
how do i do that? ive tried using this:
fid = fopen(file);
Unrecognized function or variable 'random_x'.
C = textscan(fid, '%s %s %s %d %d', 'HeaderLines',1,'Delimiter',',','EmptyValue',0);
fclose(fid);
Hz = C{4};
disp(Hz)
y = C{5};
disp(y)
but it will read the 1st line with columns and i dont want that.
i need to work with .xy files, but for help i manually converted it and attached.

Respuesta aceptada

Chunru
Chunru el 26 de Jul. de 2022
Editada: Chunru el 26 de Jul. de 2022
file='random_x.txt';
fid = fopen(file, 'rt');
i=1;
s1 = '';
while ~feof(fid)
s{i} = [s1 fgetl(fid)]; % 1st line
[xy{i}, n] = fscanf(fid, '%f, %f', inf); % xy
if mod(n, 2)==1
s1 = [num2str(xy{i}(end)) ','];
end
xy{i} = reshape(xy{i}(1:floor(n/2)*2), 2, []);
i = i+1;
end
fclose(fid);
s
s = 1×4 cell array
{'7004825,RMS:30.78,No:1082.'} {'17197987,RMS:81.89,No:916.7'} {'4104402,RMS:42.36,No:799.4'} {'1118378,RMS:51.75,No:554.2'}
xy
xy = 1×4 cell array
{2×825 double} {2×825 double} {2×796 double} {2×796 double}
  5 comentarios
Chunru
Chunru el 26 de Jul. de 2022
See updated.
Mateusz Kiersnowski
Mateusz Kiersnowski el 27 de Jul. de 2022
thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by