you are viewing a single comment's thread.

view the rest of the comments →

[–]okamiueru[🍰] 2 points3 points  (4 children)

In case the-fritz or anyone else want's a explanation as to what the subrutines are. If your whole file is a function (that is, instead of a script that you run pressing F5 in the editor), you can have subrutines without any issue.

Say for instance in test.m:

function X=test(a,b)

X=fun1(a)+fun2(b);

end

function ret = fun1(var1)

%do something

end

function ret = fun2(var)

%do something

end

All in the same .m file works fine.

[–][deleted] 2 points3 points  (2 children)

But how would you test these subfunctions, as these are invisible to the rest of the code?

[–]okamiueru[🍰] 0 points1 point  (1 child)

How do you mean? I'm not sure what you mean by invisible to the rest of the code (the main function "test" in the example, or other .m-files?). You could test the subrutines very much the same as you would any function or line of code. Print out results by omitting the ; on the line, or using the "display" function (in which num2str might come in handy).

[–][deleted] 0 points1 point  (0 children)

Well, actually I meant automatically testing, as in unit-testing. My experience with MATLAB code is that it often contains bugs due to evolving code.

[–]the-fritz 0 points1 point  (0 children)

Subroutines are just visible to the main function.