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

1 view (last 30 days)
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.

Accepted Answer

Rik
Rik on 13 Mar 2018
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).
  3 Comments
Rik
Rik on 13 Mar 2018
assignin and eval are usually solutions to problems arising from a bad data structure. If you explain what you're trying to accomplish I (or any other contributor) might be able to help with what you should do, instead of only being able to say what you shouldn't do.
AXL
AXL on 14 Mar 2018
You're right. I was thinking wrong! I solve it without eval or assignin!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!