필터 지우기
필터 지우기

execute a statement from one .m file into another .m file by specifying line number.

조회 수: 1 (최근 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개)

Jan
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
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:
  1. Move of the code of interest into a new file, third.m
  2. Have first.m call third.m
  3. 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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by