Converting Variable Name to String but Keeping Data!

4 ビュー (過去 30 日間)
Sean Farrell
Sean Farrell 2016 年 11 月 5 日
コメント済み: Guillaume 2016 年 11 月 7 日
Hi Folks! I would like to print a variable, and obviously its data, to the command window after my script finishes running. My variable is a not at all descriptive because it is nice and short so it makes my code look cleaner. However, when I print to the command window, I would like to print this variable as a better name, but having it consist of the same data to show... Any ideas on how to do this?
Thanks, Sean
  5 件のコメント
Sean Farrell
Sean Farrell 2016 年 11 月 6 日
With all due respect, I don't think you understood the question. I have my code documented, but I want a more descriptive variable name to be outputted to the command window for the user's reference. It would be less than ideal for the user to sift through the script, reading commented sections, to understand what each variable name is. Furthermore, since my code is extensive, short essays for variable names is also less than ideal.
Guillaume
Guillaume 2016 年 11 月 7 日
Oh, I understood the question perfectly well and had nothing to add to Star's and Walter's answer which showed exactly how to do what you want.
I wanted to comment solely on the portion I quoted, not the the general question. Having had to track down an obscure race condition in some colleague's code, my work was greatly hindered by having to remember what these "nice and short variable names" were meant to represent. If I don't have to engage my brain to try remember what's in each variable, I can focus it more on what the code is actually doing. (That particular case was not helped by the fact that these variables were also global. Arghhh!)
Think of us old people going senile ;)

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

採用された回答

Star Strider
Star Strider 2016 年 11 月 5 日
I’m not certain what you want, but fprintf or sprintf are possibilities:
a = rand;
asq = a^2;
fprintf('a_squared = %f\n', asq)
  3 件のコメント
Sean Farrell
Sean Farrell 2016 年 11 月 6 日
Thank you Star Strider and Walter, this is exactly what I was looking for!
Star Strider
Star Strider 2016 年 11 月 6 日
Our pleasure!

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

その他の回答 (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