How to get the highlighted parts from the text file to another separate text file?

조회 수: 1 (최근 30일)
  댓글 수: 13
dpb
dpb 2022년 5월 12일
Glad to help...teaching is part of the job...
Actually, it just dawned on me that I didn't do the best job that could have on the above code -- one can do it entirely in a vectorized form without arrayfun at all that simplifies the code quite a bit. Even w/ 30 years, one can pick the wrong function for the job--if use split instead of strsplit on the string array, one can write
>> NodeData=str2double(split(strtrim(D(ix+3))))
NodeData =
3159 36.019
1558 28.761
6466 20.324
>>
and the whole solution is fully vectorized.
MORAL: Don't feel bad in missing a better tool/function; even experienced users can either by not being familiar with the whole lexicon or simply not thinking of the better choice. split was introduced in R2016b in conjunction with the new string class and had the advantage that it is vectorized whereas the venerable strplit for cellstr and char strings prior to the introduction of strings is not...being an old-timer sometimes is a hindrance; it's the old friends that one thinks of first automagically as I did here.
>> strsplit(strtrim(D(ix+3)))
Error using strsplit (line 80)
First input must be either a character vector or a string scalar.
>>
I knew this would happen; hence the use of arrayfun to pass each individually; just whiffed on remembering split at the time.

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

채택된 답변

dpb
dpb 2022년 5월 12일
편집: dpb 2022년 5월 12일
D=readlines('SB_PB.txt');
ix=find(contains(D,'NODE FOOT'));
NodeData=str2double(split(strtrim(D(i+3))));
to simplify the solution using arrayfun in earlier comments.
NB: Use of new(ish) split() function for strings in place of venerable non-vectorized strsplit requiring the looping construct.
>> NodeData=str2double(split(strtrim(D(ix+3))))
NodeData =
3159 36.019
1558 28.761
6466 20.324
>>
As opposed to the nonvectorized strsplit
>> strsplit(strtrim(D(ix+3)))
Error using strsplit (line 80)
First input must be either a character vector or a string scalar.
>>
which thus needs the looping construct.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by