-1

I have created a .m file like this:

function M
clc
clear
T=[
1   2   3
4   5   6
7   8   9
];
surface (T)
shading interp
end

ALL I want is this: when click on that m-file(I mean when m-file opens), matlab RUN it. so there will be no need for user to click on RUN.

EDIT: I want script run when opening m-file I need this because I've created this M file at the end of a C# program, and C# program opens m-file just to shows the graph. so it is better that after opening it, run automatically and shows the graph. (so user easily see graph and there is no need to run it.) I thought there maybe an easy way , such as writing a code in m-file which makes it run right after opening.

bunNyBug
  • 146
  • 1
  • 2
  • 13
  • why -1??!! instead of answering. question is not clear?! or it shows no effort or research?! what is the problem?! – bunNyBug Feb 23 '13 at 01:08
  • Please clarify exactly what you mean. Do you want the script to run when matlab starts? Or do you want the script to run immediately when opening the file? Why not just press "run" first, or just type the name of the function in the command window and hit enter? – ThijsW Feb 23 '13 at 01:29
  • @ThijsW I want script run when opening .m file I need this because it is I've created this M file at the end of a C# program, and open it from C# so it is better that after opening it, run automatically and shows the graph. (so user easily see graph and there is no need to run that.) – bunNyBug Feb 23 '13 at 11:22

2 Answers2

0

If you are trying to avoid having the user have to manually run the function, you could have a batch file automatically start MATLAB and run the function. Create a batch file (use a text editor and save with a .bat extension)

matlab -r m

If you search for "matlab command line parameters", you can find more information and additional options.

Alternatively, you could save the figure that MATLAB produces as a .FIG and have the user load that. (Or save as a pdf and avoid having them use MATLAB altogether)

RK M
  • 55
  • 7
  • ALL I want is this: when click on that .m file(I mean when I open .m file), matlab RUN it. so there will be no need for user to click on RUN. – bunNyBug Feb 23 '13 at 12:14
  • See this post, which could be used in conjunction with my suggestion. http://stackoverflow.com/questions/1469764/run-command-prompt-commands – RK M Feb 25 '13 at 17:30
0

I have no experience with C# at all, but it seems to me like the best way to achieve what you want is to somehow call the matlab function from the C# program. I know matlab can be integrated in C programs quite easily using mex files.

You should check out this link on integrating matlab with C#, maybe it can help you. If you are able to call the matlab function directly from the C# programs, the user doesn't even have to click the m-file.

If you really want the user to have to click the m-file, but not the run button afterwards, I am assuming you are dealing with users that do not know matlab and therefore do not have matlab running already, so you want them to click the m-file in Windows (or another OS) and not in Matlab.

Then, by clicking the m-file an instance of matlab should open, with the clicked file in the editor.

You could create - or edit - the startup.m file in matlab's startup directory (see matlab documentation). This is a script file executed on startup of matlab. So if you create a startup.m in your C# program that includes a line calling your desired matlab function (M from your example), the function should be executed only after clicking the function file.

If, however an instance of matlab was already running, the startup.m script will not be called and this method will not work anymore.

ThijsW
  • 2,599
  • 15
  • 17
  • right now in my program user don't click on m-file. C# program opens m-file. I thought there maybe an easy way , such as writing a code in m-file which makes it run right after opening. but seems that it is not like that. Thank you very very much for your effort. I would love to vote up your answer if my reputation lets me! – bunNyBug Feb 23 '13 at 19:41