How to read .XY file?

조회 수: 100 (최근 30일)
Mateusz Kiersnowski
Mateusz Kiersnowski 2022년 7월 26일
댓글: Mateusz Kiersnowski 2022년 7월 27일
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.

채택된 답변

Chunru
Chunru 2022년 7월 26일
편집: Chunru 2022년 7월 26일
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
Chunru
Chunru 2022년 7월 26일
See updated.
Mateusz Kiersnowski
Mateusz Kiersnowski 2022년 7월 27일
thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by