reading "overpunched" characters

I'm reading a legacy data format, in which negative numbers were represented by the first numeral being "overpunched". i.e. 123 is 123, but -123 is J23, -234 is K34, and so on down to -987 is R87.
I'm reading long columns of ASCII numbers and converting them to the appropriate double.
Can anyone suggest an efficient way of making this conversion (besides looping through find(char=='J') etc. and replacing with correct signed numbers?)

 採用された回答

Walter Roberson
Walter Roberson 2011 年 3 月 22 日

0 投票

Wow, I haven't encountered that scheme since my Hollerith days!
regexprep() can do all the changes at once, in a single command.
regexprep('([J-R])','-${char(''$1''-25)}')
Question: usually when I encountered that overpunch format, it was in the context of a fixed-width field that might not have any space before the overpunched character. Is that the situation here? If it is, then the situation gets more complicated as the addition of the '-' moves the field boundaries.

2 件のコメント

Glenn Meurer
Glenn Meurer 2011 年 3 月 23 日
Yes - fixed width. (And Thanks for the tip!)
I've read the several affected fields into character arrays, and think I can do something like prepend a 0 then replace '0J'. They threw a curveball, in that some of the entries have legitimate '-' signs, so might have to replace those first with '-0', then the letters.
I've got to smarten up on regular expressions -
when I say s=regexprep('J64830', '([J-R])','-${char(''$1''-25)}');
I get "-[VRLUF64830"
Thanks again for your suggestion
Glenn Meurer
Glenn Meurer 2011 年 3 月 24 日
I misinterpreted the doubled quotes... this works:
regexprep('([J-R])','-${char($1-25)}')

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by