How to read .XY file?

78 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(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