1

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.

1
  • Load is the one you are looking for when trying to include more files to your code. Then you'll just need the input from user. Look at the example 1: mathworks.se/help/techdoc/ref/eval.html Commented Dec 27, 2011 at 21:21

2 Answers 2

2

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'

Sign up to request clarification or add additional context in comments.

1 Comment

The OP asked for a .m not a .mat (but the principle is the same)
1

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.

1 Comment

If a graphical solution is acceptable, inputdlg is a very nice way to do it, the link provided is a clear and simple tutorial and evan or run can then be used, as described in both answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.