MATLAB: How to save the name of a variable and a folder using -printf

directororyfolderMATLABprintfsprint

I want a certain folder to be created according to the number of elements given by the user.
dir_Cantilever_EPD_3000Elem = [pwd ...
'\Results\Cantilever Beam Domain\3000 Elements\EPD']
I want the variable named and the name of the folder to be like that:
sprintf('dir_Cantilever_EPD_%dElem',fem.NElem)
where fem.NElem is the number of elements given. So I do not have to do lots of 'if' to check the number os elements.

Best Answer

You can do this with eval, but you shouldn't. Use a cell array instead.
(I have taken the liberty of adapting some of Stephen Cobeldick's thoughts on this topic) Introspective programing (eval and other functions like it) is slow, and the MATLAB documentation warns specifically against it. Using eval is slow, makes debugging difficult, and removes all code helper tools (code checking, syntax hints, code completion, variable highlighting, etc.). A longer discussion can be found here. If you are not an expert and you think you need eval, there are probably better solutions to your problem. Also, if you are not an expert, you might find interesting and helpful hints on this page (and if you are an expert, you can still learn from it).
Related Question