MATLAB Answers


Help with Code Problem

Asked by Bilal Bingolbali on 25 Mar 2016
Latest activity Edited by Stephen Cobeldick on 19 Jun 2019
for k=1:0.1:5;
eval(['HT_' num2str(k) '=H;']);
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.


Show 1 older comment
Yeah I am new to Matlab and as of now I aint got any other option apart from eval code...what do u suggest I do?.Thanx angain.
Walter Roberson
on 25 Mar 2016
If it worked, what would you like the result to be for k = 1.2 (for example) ?

Sign in to comment.


1 Answer

Star Strider
Answer by Star Strider
on 25 Mar 2016

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.


Sign in to comment.