How do I read only numerical data and ignore other text from input file?
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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
2017-11-12
I think the idea was to parse the strings to automatically figure out the variable names.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Language Support 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!