1

I have a .m script file which contains 95% of my work. Part of my assignment was to write a function that performs a certain calculation and then utilize the function for various examples.

I created a separate .m file for the function and named it after the function. I then utilize the function in my main .m script, and everything works fine. I published the .m script and everything looks fine.

However, it does not include my function I created. Since this is part of my work, I need to turn this in along with my main script file. I assume that I need to publish this separately since I do not see a way to include it in the original publication.

Here is my matlab function

%% Function rvm
function [y1,y2] = rvm(x,y)
    discr = sqrt((x^2)-(4*y));  
    y1 = (-x-discr)/2;
    y2 = (-x+discr)/2;
end

and here is the error I get when publishing:

Not enough input arguments.

Error in rvm(line 4)
    discr = sqrt((x^2)-(y*c));  

I haven't used matlab extensively, and it's been a while since then so I am having a little trouble debugging this issue.

I'm confused how this can be giving me an error, since my main script publishes with no errors and utilizes the function fine. Also the syntax seems to be fine...

Suever
  • 64,497
  • 14
  • 82
  • 101
Daven.Geno
  • 21
  • 4

2 Answers2

2

Or you can simply add the following markup to your main script :

%%
% <include>rvm.m</include>

This will render the code of the function with Matlab syntax coloration.

NicoBernard
  • 362
  • 2
  • 8
0

The issue is that by default, publish will call your function with no input arguments which is causing the error. If you want to assign values to x and y for publication in your work, you can specify this using the options struct to publish, specifically the codeToEvaluate option

opts = struct('codeToEvaluate', 'x = 1; y = 2;');
publish('rvm', opts)

enter image description here

Alternately, you could use one of the file exchange submissions that allows you to include subfunctions within your published output.

Suever
  • 64,497
  • 14
  • 82
  • 101
  • hmmm so publishing a function automatically calls the function? I don't want to set input arguments because the function is already called throughout my main script.... Is my most reasonable option just to print the script out normally..? – Daven.Geno Feb 06 '17 at 19:38
  • MATLAB's `publish` does not print functions which are *called* by your function (unless you use one of the file exchange submissions I linked to). You can publish just the sub function using the code that I have provided above. You can also just simply print the .m file if you don't need all of the functionality of `publish`. – Suever Feb 06 '17 at 19:43