LoginSignup
5
1

More than 5 years have passed since last update.

他言語からMatlabに来た人がハマりそうなlength()の話

Last updated at Posted at 2019-02-19

物の見事にハマった

lengthで文字列の長さを取ろうとしたら思ったようにとれず、コマンドウインドウでこんなコマンドを叩いてみた。

コマンドウインドウ
> hoge = "somestring";
> length(hoge)

  ans =
        1

違う、そうじゃない。

stringで文字数が欲しい場合はstrlengthで。

lengthではなくstrlengthを使うと文字数が取れます。

コマンドウインドウ
> hoge = "somestring";
> strlength(hoge)

  ans =
        10

やったぜ

そもそもstringは最近実装された型

Matlabのドキュメンテーションで確認すると、stringが実装されたのは「R2016b」(=2016年後期)。
めちゃんこ最近です。
文字配列を数値配列に変換するやり方として「str2num」や「str2double」がありますが、「strだからstringだろ」とstringを突っ込むと前者はエラー、後者は空の行列になります。

Matlabで文字列を扱うときはcharを使いましょう

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1