How to ignore letters in a numeric cell?

31 ビュー (過去 30 日間)
Daniel Tanner
Daniel Tanner 2020 年 1 月 28 日
コメント済み: Daniel Tanner 2020 年 1 月 29 日
I have imported a vector into MatLab from excel which was originally imported from ANSYS. I would like to plot this vector against another one, however each entry has a 'mm' after it, like so:
PinRadius = [0.01mm;0.02mm;0.03mm;0.04mm;...]
Is there a way in MatLab to ignore the letters and just read the numbers so I can produce a plot? I know I could manually do this but I have over 200+ entries.
Any help is greatly appreciated, thanks!
  2 件のコメント
Adam
Adam 2020 年 1 月 28 日
Are they in cell arrays or a char array (implying that every one is the same length and never more nor fewer digits)?
The example format you have given is not valid as it is a numeric array with characters in it too.
Daniel Tanner
Daniel Tanner 2020 年 1 月 28 日
It is in a cell array. They go from 0.01mm to 2.9mm.

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

採用された回答

Adam
Adam 2020 年 1 月 28 日
編集済み: Adam 2020 年 1 月 28 日
cellfun( @(x) str2double( erase( x, 'mm' ) ), pin_radius )
should convert them to an array of doubles.
If you are using pre R2016b you would have to use something like this instead:
cellfun( @(x) str2double( strrep( x, 'mm', '' ) ), pin_radius )
(Note in that version the 3rd argument to strrep is two consecutive single quotes, not a double quote)
  1 件のコメント
Daniel Tanner
Daniel Tanner 2020 年 1 月 28 日
Perfect, thank you!

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

その他の回答 (1 件)

Stephen23
Stephen23 2020 年 1 月 28 日
Simpler and much more efficient with sscanf:
>> C = {'0.01mm';'0.02mm';'0.03mm';'0.04mm'};
>> V = sscanf([C{:}],'%fmm')
V =
0.010000
0.020000
0.030000
0.040000
  1 件のコメント
Daniel Tanner
Daniel Tanner 2020 年 1 月 29 日
Thank you!

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

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by