MATLAB Answers

How to ignore letters in a numeric cell?

조회 수: 5(최근 30일)
Daniel Tanner
Daniel Tanner 28 Jan 2020
댓글: Daniel Tanner 29 Jan 2020
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 28 Jan 2020
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 28 Jan 2020
It is in a cell array. They go from 0.01mm to 2.9mm.

Sign in to comment.

채택된 답변

Adam
Adam 28 Jan 2020
편집: Adam 28 Jan 2020
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 28 Jan 2020
Perfect, thank you!

Sign in to comment.

추가 답변(1개)

Stephen Cobeldick
Stephen Cobeldick 28 Jan 2020
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 29 Jan 2020
Thank you!

Sign in to comment.


Translated by