how to extract data from ascii file?

조회 수: 6 (최근 30일)
Rakesh Roshan
Rakesh Roshan 2022년 5월 25일
댓글: Voss 2022년 5월 25일
i have been given ascii file ..
how to read data from it and plot the graphs
in this file around 2000 data is present ...and i need only 1001 data in such a way that i don't lose the entire data only some of them.
for example from freq=8-12GHz there are data like
freq parameter
8 -0.00000003
8.001 -0.2233652
8.002 -2.0000012
;
;
10.222 -2.22333
;
;
12 -2.0001123
so basically from 8-12 GHz all the data but losing very few data
2018a version
  댓글 수: 2
Stephen23
Stephen23 2022년 5월 25일
@Rakesh Roshan: please upload a sample data file by clicking the paperclip button.
Rakesh Roshan
Rakesh Roshan 2022년 5월 25일
i have uploaded the data sir

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

답변 (1개)

Voss
Voss 2022년 5월 25일
data = readmatrix('qwer.txt')
data = 31×2
8.0000 0.0003 8.0100 0.0003 8.0200 0.0003 8.0300 0.0003 8.0400 0.0004 8.0500 0.0004 8.0600 0.0004 8.0700 0.0004 8.0800 0.0006 8.0900 0.0008
plot(data(:,1),data(:,2),'.-')
"in this file around 2000 data is present ...and i need only 1001 data"
data = data(1:2:end,:) % keep rows 1, 3, 5, ...
data = 16×2
8.0000 0.0003 8.0200 0.0003 8.0400 0.0004 8.0600 0.0004 8.0800 0.0006 9.0000 0.0009 9.0200 0.0001 9.0400 0.0006 9.0600 0.0004 9.8000 0.0045
hold on
plot(data(:,1),data(:,2),'or')
  댓글 수: 3
Voss
Voss 2022년 5월 25일
Please see this answer:
https://www.mathworks.com/matlabcentral/answers/57446-faq-how-can-i-process-a-sequence-of-files#answer_69523
You can alter the code to read just the files you want.
Voss
Voss 2022년 5월 25일
For instance, you might store Freq and Sparamters from the files in separate fields in the struct array returned from dir:
files = dir('*.txt');
disp({files.name});
{'qwer.txt'} {'qwer2.txt'} {'qwer3.txt'}
files = files(1:2:end); % just keep every other file: files(1), files(3), ...
disp({files.name});
{'qwer.txt'} {'qwer3.txt'}
for ii = 1:numel(files)
temp = readmatrix(files(ii).name);
files(ii).xdata = temp(:,1);
files(ii).ydata = temp(:,2);
end
plot_args = [{files.xdata}; {files.ydata}];
plot(plot_args{:});

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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by