MATLAB Answers

Any trick to name a variable with decimal number?

6 ビュー (過去 30 日間)
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019 年 12 月 3 日
コメント済み: Guillaume 2019 年 12 月 3 日
I noticed that decimal number cant be used as the name of variable like "trd_0.45". Any trirck to solve the issue? I prefer to have it as it is and not convert it to integer.

  3 件のコメント

Guillaume 2019 年 12 月 3 日
As Stephan answered that's not possible.
But more importantly, it sounds like you're trying to embed some sort of metadata within the variable name. It's an extremely bad idea. Metadata is data, and as all data should be stored as the content of a variable, not its name.
Zeynab Mousavikhamene
Zeynab Mousavikhamene 2019 年 12 月 3 日
Would you please explain why it is bad? I was thinking it will reduce the chance of making mistake when there are thousands of files. In my example, 0.45 is the most importatnt data to indicate the content of the file.
Guillaume 2019 年 12 月 3 日
Embedding properties in variable names is always a bad idea. it prevents from doing any manipulation based on the value of that property. E.g. what if you want to look at all the files for which the property is between 0.4 and 0.5, how do you do that when it's embedded in the variable name? Instead if you embed it as a property of a structure:
%get all trd whose important property (whatever that is) is in the range [0.4, 0.5):
filteredtrd = trd([trd.yourimportantproperty] >= 0.4 & [trd.yourimportantproperty] < 0.5);
Or what if you want to process each of these trd one by one with the same function. Since they all have different variable names, you can't loop over them. Separate the variable name from the property and it's as simple as:
for i = 1:numel(trd)
result = dosomething(trd(i));
There are many ways to store properties (fields of a structure, separate variable,, all better than storing it in the variable names. Which way you store these properties depend on exactly what you're trying to do.

サインイン to comment.

回答 (1 件)

Stephan 2019 年 12 月 3 日
編集済み: Stephan 2019 年 12 月 3 日
No, it is simply not allowed in Matlab. No way, no trick:

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。


Translated by