How do I read only numerical data and ignore other text from input file?

조회 수: 3 (최근 30일)
Siddharth Sharma
Siddharth Sharma 2017년 11월 3일
댓글: Walter Roberson 2017년 11월 12일
Input file looks like this. Is there a way to ignore the specifiers? Is there a way the program allocates variables as given in the file?
0, 1 !t0, y0
1 !tf
0.1 !h
1 !Euler Forward
  댓글 수: 2
Jan
Jan 2017년 11월 3일
It would be helpful, if you explain what you expect as output.
Siddharth Sharma
Siddharth Sharma 2017년 11월 11일
Hi, these numbers are to be allotted to the variables next to the exclamation sign. This is just the input, I don't think the output matters but anyway, my code is supposed to numerically solve ODEs so the program will use Runge-Kutta 4th order algorithm(written in the code).

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

답변 (1개)

Akira Agata
Akira Agata 2017년 11월 12일
Is this what you would like to do?
% After textscan
C = {'0, 1 !t0, y0';...
'1 !tf';...
'0.1 !h';...
'1 !Euler Forward'};
% Extract numbers and store them in variables for each line
tmp = regexp(C{1},'[+-]?(\d+\.?\d*|\.\d+)','match');
t0 = str2double(tmp{1});
y0 = str2double(tmp{2});
tmp = regexp(C{2},'[+-]?(\d+\.?\d*|\.\d+)','match');
tf = str2double(tmp{1});
tmp = regexp(C{3},'[+-]?(\d+\.?\d*|\.\d+)','match');
h = str2double(tmp{1});
tmp = regexp(C{4},'[+-]?(\d+\.?\d*|\.\d+)','match');
EulerForward = str2double(tmp{1});
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 11월 12일
I think the idea was to parse the strings to automatically figure out the variable names.

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by