How do I find specific values in a .txt file?

조회 수: 15 (최근 30일)
Ryan
Ryan 2021년 2월 1일
댓글: Mathieu NOE 2021년 2월 1일
Hello everybody,
I have a .txt file. And I need to find the key words in the text then pick the value next to the key words. Here is one part of this text file:
%% Define the target filter
freq_step = 0.2; % Frequency step (GHz)
lowest_freq = 20; % Lowest frequency of the whole frequency band (GHz)
mid_freq = 33; % Middle frequency of the passband (GHz)
highest_freq = 45; % Highest frequency of the whole frequency band (GHz)
pass_s11_max_tgt = -10; % Target maximum S11 in the passband (dB)
pass_s11_min_tgt = -15; % Target minimum S11 in the passband (dB)
pass_s21_tgt = -1; % Target S21 in the passband (dB)
stop_s11_tgt = -1; % Target S11 in the stopband (dB)
stop_s21_max_tgt = -15; % Target maximum S21 in the stopband (dB)
stop_s21_min_tgt = -25; % Target minimum S21 in the stopband (dB)
I need the values after "freq_step = ", "lowest_freq = ", "highest_freq = ". Could anyone help me with that?

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 2월 1일
hello
this code will extract the searched items and stote them in a table (taht you can save to excel if needed)
hope it helps
lines = readlines('Document1.txt');
for ci =1:numel(lines)
line_str = extractBefore(lines(ci),'='); % extract question number (with double quote)
if strfind(line_str,'freq_step')
Value1 = str2num(extractBetween(lines(ci),'=',';')); % extract values and convert string to numerical
elseif strfind(line_str,'lowest_freq')
Value2 = str2num(extractBetween(lines(ci),'=',';')); % extract values and convert string to numerical
elseif strfind(line_str,'highest_freq')
Value3 = str2num(extractBetween(lines(ci),'=',';')); % extract values and convert string to numerical
end
end
A = [Value1' Value2' Value3'];
T = array2table(A, 'VariableNames',{'freq step','lowest freq','highest freq'})
writetable(T,'test.xlsx')
  댓글 수: 2
Ryan
Ryan 2021년 2월 1일
Amazing! It works!
You helped me a lot! And I learn a lot from it!
Thanks sincerely!
Mathieu NOE
Mathieu NOE 2021년 2월 1일
you're welcome

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by