MATLAB merging the codes

조회 수: 1 (최근 30일)
Ali
Ali 2016년 11월 7일
댓글: James Tursa 2016년 11월 7일
Hello, I have a question about choosing the cases in MATLAB. I have four different calculations and equations. They all start with the same set of variables. There is a couple of different equations and after those parts, they all continue with the same lines of codes after that point. All I need is that;
a=... b=... c=...
[....... ........ DIFFERENT EQNS FOR FOUR DIFFERENT CASES]
[SAME LINES AFTER THAT]
I have four different .m files at the moment. I want to merge them into one.
Thanks in advance!
Regards.
  댓글 수: 1
James Tursa
James Tursa 2016년 11월 7일
편집: James Tursa 2016년 11월 7일
How do you determine which case to run? Is there a variable you use for switching between them? Is this determined from the inputs (a,b,c,...) themselves?

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

답변 (2개)

Ali
Ali 2016년 11월 7일
Actually, I am not choosing any. I will need to run all four of them. I just do not want to type all of the same lines four times. I am going to have to change all of the variables of the same part. It would look like that ;
CASE 1 [..... .....] CASE 2 [...... .....] . . . [SAME PARTS]
  댓글 수: 1
James Tursa
James Tursa 2016년 11월 7일
I still don't understand. Are you asking how to do something like this?
a=... b=... c=...
CASE 1 code
SAME PARTS
CASE 2 code
SAME PARTS
CASE 3 code
SAME PARTS
CASE 4 code
SAME PARTS
without copying all of the SAME PARTS code? Or what?

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


Ali
Ali 2016년 11월 7일
Yes, this is exactly what I am trying to simplify. But well, when I write these same parts once and four cases one under the other. When I copy/paste all of the codes, It will give me the result of the last one. CASE 4 in this case. I want all of them to be calculated at same parts section. CASE 1 + SAME PARTS, CASE 2 + SAME PARTS ....
  댓글 수: 1
James Tursa
James Tursa 2016년 11월 7일
Without seeing your code, all I can offer is that you put the SAME PARTS code in a function and call it with your various inputs, putting the results into different variables (or maybe different parts of a cell array or struct).

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by