How to read .XY file?

99 views (last 30 days)
Mateusz Kiersnowski
Mateusz Kiersnowski on 26 Jul 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.

Accepted Answer

Chunru
Chunru on 26 Jul 2022
Edited: Chunru on 26 Jul 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 Comments
Chunru
Chunru on 26 Jul 2022
See updated.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!