フィルターのクリア

Transformation form char to double

127 ビュー (過去 30 日間)
jrbbrt
jrbbrt 2018 年 8 月 9 日
コメント済み: jrbbrt 2018 年 8 月 9 日
Hi everyone .. I know this should be a simple one, but somehow I'm doing hard on this task ...
I got: A row of chars representing numbers, which for example look like this '1799', '1,867', '0,219', '-0,719502', etc. and are saved in cells (200x1 cell).
So what I want to do: is converting those chars into the equivalent numbers they're representing!
My idea:
  1. was - due them being in a cell - using cell2mat ... which isn't working because Matlab says "Dimensions of arrays being concatenated are not consistent.".
  2. So my next thought I had was simply using str2double ... here the problem is that all the commas are deleted and for example instead of '0,219' I end up with the number 219. Not what I need.
  3. Elsewise I had the idea of using strsplit after using char on my row. Unfortunately here Matlab says my "'STRING' input must be either a char row vector, a cell array of char row vectors, or a string array."
  4. So my latest approach is using:
str2double(char(myrow(:,1)'));
... which results in 'NaN' as my answer. What am I doing wrong?
I appreciate your thoughts! Best regards.

採用された回答

Stephen23
Stephen23 2018 年 8 月 9 日
編集済み: Stephen23 2018 年 8 月 9 日
You need both strrep and str2double:
>> C = {'1799', '1,867', '0,219', '-0,719502'};
>> str2double(strrep(C,',','.'))
ans =
1799.00000 1.86700 0.21900 -0.71950
Native MATLAB only uses decimal point as the decimal radix character, never a comma, so you will always have to convert this explicitly.
  1 件のコメント
jrbbrt
jrbbrt 2018 年 8 月 9 日
Thanks for your quick respond and explanation Stephen!

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

その他の回答 (0 件)

カテゴリ

Help Center および 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