MATLAB: Alternative for eval, for the case

evalnum2str

Hellow, I have a lot off variables witch I want to rename from the name they have to the name they have + a new piece witch I define by input of X(to be abel to easy change it). For example now I have typed:
A4optie6a33=A4;
A5optie6a33=A5;
A6optie6a33=A6;
Kostoptie6a33=Kost;
Onderhoudskosten1optie6a33=Onderhoudskosten1;
Onderhoudskosten2optie6a33=Onderhoudskosten2;
Onderhoudskosten3optie6a33=Onderhoudskosten3;
RAAMoptie6a33=RAAM;
Restwaarde1optie6a33=Restwaarde1;
I taught of doing it like this:
X='optie6a33';
eval(['A4' num2str(X) '=A4']);
eval(['A5' num2str(X) '=A5']);
eval(['A6' num2str(X) '=A6']);
...
But this calculation take's al lot of time for watch it has to do I think.
Thanks in advance!

Best Answer

Both eval and num2str are slower than many of their alternatives. Also, I don't see why you're using num2str anyway, since your variable X is already a string.
In any case, you could try using inputname and assignin to rename variables more efficiently. For example, here's a function that will rename a variable by appending a suffix:
function renameWithSuffix(value, suffix)
newName = [inputname(1) suffix];
assignin('caller', newName, value);
end
You could then rename A4 to A4optie6a33 as follows:
renameWithSuffix(A4, 'optie6a33');
Related Question