Execute/load multiple .m file in single main .m file

Hi, recently I build multiple program which each of them has their own m file. For example, there are a.m, b.m and c.m. What I want to do is to execute each of the program in single main program in .m (Let say main.m). However, I could not achieved the solution yet.
%%%%Main program %%%%
run(a.m);
run(b.m);
run(c.m);
The result coming out is:
Undefined variable "a" or class "a.m".
Each m file has coding that will give out output in array or cell. Is there more effective solution that enable me to execute m coding from other m file in my main m file? Perhaps on what I need is retype all coding from all separate file and write it into single main.m?
Thank you

 採用された回答

Birdman
Birdman 2018 年 3 月 19 日
編集済み: Birdman 2018 年 3 月 19 日

2 投票

Just simply write
a
b
c
which are the names of your m files.

1 件のコメント

Muhammad Bariq Azmi
Muhammad Bariq Azmi 2018 年 3 月 19 日
Thanks! Never thought that I'm actually overthinking despite the solution is much more direct to the point

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2018 年 3 月 19 日

3 投票

The problem is, that in run(a.m) "a.m" is interpreted as the field "m" of the struct "a". Therefore you get this message, which is clear actually:
Undefined variable "a" or class "a.m"
You want "a.m" to be a char vector containing the name of the script instead. Then you need quotes:
run('a.m')
Using the direct form:
a
is much easier, but with the run() command you can define the path of the script also.

5 件のコメント

Fawad Chandio
Fawad Chandio 2019 年 9 月 23 日
where i put "Run(a.m)" behend any button or where...??
Jan
Jan 2019 年 9 月 23 日
What do you want to achieve? Which button?
I have explained, why run(a.m) does not work. So I think, you should not use it at all.
Arif Hoq
Arif Hoq 2021 年 12 月 15 日
just for the curiosity. in my case i have 4 .m files.Mmatlab can execute all files. but in the workspace browser its showing only the last file result.
Jan
Jan 2021 年 12 月 15 日
Does this have a realtion to the question of this thread? If not, ask a new question, please.
Maybe the .m-files are scripts starting with a clear command. Then all former results are deleted.
Arif Hoq
Arif Hoq 2021 年 12 月 16 日
exactly that is the reason. thanks a lot @Jan.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by