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

2 vues (au cours des 30 derniers jours)
  13 commentaires
dpb
dpb le 12 Mai 2022
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.
Subaharan Rajenthirakumar
Subaharan Rajenthirakumar le 13 Mai 2022
Thank you for the help and explanation.

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 12 Mai 2022
Modifié(e) : dpb le 12 Mai 2022
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.

Plus de réponses (0)

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by