execute a statement from one .m file into another .m file by specifying line number.
3 次查看(过去 30 天)
显示 更早的评论
Guys , I have two .m files. I wish to execute certain part of code from my first file in my second file.Is there a way I can do it by specifying in second file, to execute certain statements of the first file by mentioning the line numbers at which my statement is written in first file?
1 个评论
回答(1 个)
Jan
2015-3-8
No. And this would be a really ugly way of programming. It would be a GOTO command, and we are so lucky that Matlab offers a celan and structured way to avoid such methods.
Simply insert the needed code section by copy&paste or move it to a dedicated function.
1 个评论
Ken Atwell
2015-3-8
To expound on this: Let's say you have the file first.m and second.m. Rather than try to find a way to call a subset of second.m (which MATLAB doesn't -- and shouldn't -- allow), do the following:
- Move of the code of interest into a new file, third.m
- Have first.m call third.m
- Have second.m also call third.m
You might be able to get away with keeping all three files as simple scripts (that is, MATLAB files that don't begin with a function statement), but at some point you will want to learn about functions as a better way of reusing code. Read Scripts vs. Functions when you have a moment.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!