Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way of selectively including code when publishing in Matlab?

Tags:

matlab

I'm writing MATLAB code in order to publish it later. By publishing, I mean the built-in MATLAB publish tool that allows the programmer to make a full report generated from their MATLAB code. There's an option to include the code with this report, section by section, preceding the results of this code. Is there a way to tell MATLAB to include some of this code in the report but not all of it? I know there are quite a few markup code tags, but I wasn't able to find anything on this topic.

Edit: Just to clarify, I want all results to be published, but only some of the code. So simply removing this code is not an option.

Cheers! = )

like image 500
Phonon Avatar asked Aug 31 '11 15:08

Phonon


People also ask

How do I insert MATLAB code into Word?

The easiest way to embed a MATLAB figure is to simply copy the figure (in the MATLAB figure window go to Edit->Copy Figure) and then paste into Word directly.

How do I save a published MATLAB code as a PDF?

Answers (1) If you're working with a traditional MATLAB file (. m file), then you should see a "PUBLISH" tab. If you're working with a Live Script, then you can export to a PDF from the Save button.


2 Answers

Hide your code that you don't want people to see in a script. For example, in the "sine_wave" example from the publish documentation page, I added a single line:

junk

Here's the content of junk:

figure()
plot(0:0.01:6,sin(0:0.01:6))

Now run your main script, and the published result has "junk" in the listing, but the contents of junk are not included, and you get the nice version of a sine wave, instead of the crappy one included in their example.

like image 168
John Avatar answered Nov 16 '22 01:11

John


The only way I know of to do this is to remove the code that you don't want to appear in the output. If you just want to display the code and not the output, then you can just set the evalCode property to false in your call to publish.

If you do want the code to be evaluated, and the output to be published as well, then it's just slightly more complicated. You can manually execute the parts of the script that you don't want to publish, then publish the code that you care about (by putting it in it's own .m file). It shouldn't matter if the published code depends on any variables that are initialized in the omitted code, since those variables were added to your workspace when you manually executed the omitted code fragments.

Edit:

Since you've clarified your question to state that you're interested in publishing some of the code, but all of the output, I would think that your best bet is to just modify the "temporary" script (which contains the partial set of code that you wish to publish) to include any fprintf, disp, etc. function calls that you want to have appear in the output.

It's a bit hack-ish, but like I said, I'm not aware of any way to get that kind of fine granularity with "annotations" or using the publish command.

Hope that helps!

like image 33
Mansoor Siddiqui Avatar answered Nov 16 '22 02:11

Mansoor Siddiqui