Brooke
Brooke

Reputation:

How to invoke a MATLAB script in runtime by its name

Suppose I have 3 m-codes:

code1.m code2.m code3.m

and I want a code for MATLAB to "draw them together" in the sense that when we run the program, we are prompted with, say, "enter code:", then the user types in say "code3" and then code3.m is run.

I am pretty sure there is a simple code to do that, though I can't remember it.

Upvotes: 1

Views: 106

Answers (2)

Cory Dolphin
Cory Dolphin

Reputation: 2670

There are two portions to this question, the first of which is getting user input: Matlab allows you to request user input as shown in this tutorial: http://www.mathworks.com/help/techdoc/ref/input.html

strResponse = input(prompt, 's')

Part two is simply loading the file and executing it, as described by @MetalRain http://www.mathworks.com/help/techdoc/ref/eval.html

eval(['load code' strResponse '.m'])

Noting that matlab perform string concatanation on the vector for you, so the result for the input of strResponse = 1 is 'load code1.m'

Upvotes: 2

Clement J.
Clement J.

Reputation: 3032

run or eval can do it. You get the name of the file from input.

A (maybe) less flexible but safer method is to use the graphical version of input named inputdlg.

Upvotes: 1

Related Questions