Help with Code Problem

15 ビュー (過去 30 日間)
Bilal Bingolbali
Bilal Bingolbali 2016 年 3 月 25 日
編集済み: Stephen23 2022 年 2 月 15 日
for k=1:0.1:5;
eval(['HT_' num2str(k) '=H;']);
end
This code works for odd numbers(ie 1,2,3,etc) but it wont work with decimal numbers(i.e 1.1,1.2, etc). Its giving an error every time I try. Can anyone please come to rescue? Thanks in advance.
  4 件のコメント
Walter Roberson
Walter Roberson 2016 年 3 月 25 日
If it worked, what would you like the result to be for k = 1.2 (for example) ?

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

回答 (1 件)

Star Strider
Star Strider 2016 年 3 月 25 日
All that said, if you want decimals from num2str, you have to specify a format descriptor:
num2str(k, '%3.1f')
That will work, although I have no idea if it will work in the code you posted.
There is a legitimate reasons to use eval, the most significant being that you have a series of dynamically-created variables that you did not yourself create and that you are saving to a matrix in order to correct the original error in creating them, but beyond that dynamically-created variables are to be avoided.

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by