Floor function with a space giving an array output

7 ビュー (過去 30 日間)
Alfie Green
Alfie Green 2020 年 9 月 24 日
コメント済み: Alfie Green 2020 年 9 月 26 日
I was playing around with the floor function and put a space after it followed by a number in square brackets. This gave me the interesting output of an array which seemed to have nothing to do with the number I had put in.
>> floor [17.23]
ans =
91 49 55 46 50 51 93
Having played around with it for a bit, it also worked for curly braces and without any form of bracket at all. From trying different things, I think that it somehow produces an array of the number assigned to each symbol (like in UNICODE), but I don't know why and I can't find anything in the documentation.
>> floor 1
ans =
49
>> floor [1]
ans =
91 49 93
It also had the same problem when doing it with variables:
>> a = 5
a =
5
>> floor [a]
ans =
91 97 93
Does anyone know why this is happening? Or whether it is even meant to happen or if it is a bug?

採用された回答

Stephen23
Stephen23 2020 年 9 月 24 日
編集済み: Stephen23 2020 年 9 月 24 日
Your example uses command syntax, which is explained here:
The equivalent function syntax is this:
floor('[17.23]')
Because character codes do not have fractional parts, the floor operation does not change any of the character codes, so your example is exactly equivalent to simply listing the character codes themselves:
double('[17.23]')
or even just
+'[17.23]'
"Does anyone know why this is happening?"
Due to MATLAB's origin as an interactive wrapper around Fortran, and the coding fashions at the time.
"Or whether it is even meant to happen or if it is a bug?"
Command syntax has been there right from the very start of MATLAB. I recommend avoiding it.
  3 件のコメント
Steven Lord
Steven Lord 2020 年 9 月 24 日
Command syntax has been there right from the very start of MATLAB. I recommend avoiding it.
There are places where it is appropriate and places where it is not. I wouldn't recommend avoiding it entirely, just use it for functions with inputs that are supposed to be text data:
help plot
doc ode45
hold on
close all
save allTheVariables.mat
Using it for functions that expect numeric data ... yeah, that's probably better to avoid.
Alfie Green
Alfie Green 2020 年 9 月 26 日
Thanks for the help, I'll keep that in mind in the future.

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by