I want to publish my assignment with matlab.
At the start of the program, I want to print the code of my scripts to show the teacher what my functions do.
How can I do this?
So far the best way I do this is by making a dummy call at the start without ending the function call with ";"
However I'd really like to just print the code at the start.
You can use the type command:
type fileName.m
This will print the code, similarly to creating a variable and not using a semicolon to see the value of that variable.
There are tools in MATLAB for publishing code. You could conceivably even write a report in MATLAB code and publish that! Take a look at publishing MATLAB code and the documentation of the function publish
If you are interested to print particular lines from a .m file, look into dbtype.
Illustration from Mathworks site linked above. This:
becomes this:
Related
converting dicm to nii works great with the help of this script from xiangruili :
https://github.com/xiangruili/dicm2nii/blob/master/dicm2nii.m
BUT I need to modify the output filenames and add a string to it. The function save_json of the script (dicm2nii.m) was promising, but I am new to matlab and have the feeling that there is a simple solution to this problem.
Cant somebody help me, please!
Thanks!
As #Wolfie correctly mentioned, this is not something easily addressed for people unfamiliar with the particular program. But I took a very quick look since I currently use other tools for DICOM to NiFTI conversion and was curious about this one. Here are some general comments that hopefully will help.
The "json" file is just for metadata, and you probably care more about the .nii image file (or both). It looks like nii_tool('save', nii, filename, force_3D) handles the latter.
The nii_tool and save_json calls are just passed a variable containing the output filename, which you could create/modify using any of the standard MATLAB methods (e.g., sprintf or strcat). There are already some examples of this within the code in the calls to nii_tool('save', ...).
Since you say that you are new to MATLAB, it is probably easiest for you (and likely everyone) to write a script to rename the files after exportation. That way, you don't have to worry about catching all of the cases/ instances within the 3000 lines of code that someone else wrote and just fix it with a simple program at the other end. Becomes a much more tractable problem that way.
As an aside, I currently use dcm2niix ( available from GitHub or NITRC) for this conversion outside of MATLAB.
I recently started using Live editor in MATLAB and I inserted a function inside it. But, apparently, I cannot execute that particular section of code where I type function. Even the section break disappears.
Is it that using function is not suitable for live editor?
Apparently the MATLAB parser did not join the 20th century until partway through 2016, and could not interpret function definitions in scripts (live or otherwise) until R2016b. In the web-based docs, there is a notice at Add Functions to Scripts, but it took me a while to find this out because the builtin docs in R2016a or earlier do not explicitly contain this information. It is implied by the tutorials that tell you to create a new file for each function (which to me, a python programmer, sounds more like strange advice than a restriction).
Trying to define a function in a live script gives confusing errors. For example, if you create a cell with this content:
function y = myfunc(x)
y = 2*x;
end
It will underline the keyword function with a popup error that reads:
Parse error at FUNCTION: usage might be invalid MATLAB syntax.
Might be? Whom shall I ask? Upon running the cell, it prints an error after the first line:
All functions in a script must be closed with an end.
I eventually made this discovery myself thanks to a helpful message if the first thing you happen to do in a new empty live script is to start typing function on the first line; as soon as you hit the spacebar a message pops up at the top saying:
Functions and classes are not supported in the Live Editor. To continue, save the file as a plain text code file (.m).
It should work as when you add a function inside a script. For example, like this:
What function are you exactly trying to code?
The publish function in MATLAB works for both scripts and functions,
while the one for Octave works only for scripts: if I enter
publish myFunc.m
Octave gave
error: publish: Only Octave script files can be published.
Am I able to publish a function in Octave? If yes, how?
You can use Octave Forge generate_html package which is meant to generate html of individual functions. It is mostly used to generate the documentation of Octave Forge packages, and its defaults reflect that, but you could add any style you want.
This package will use the same help text that the help function in Octave sees which is the first block of comments in the file that does not start by Copyright or Author. You can have it in plain text but for nicer formatting, you can use Texinfo. In that case, the first line of the help text should be -*- texinfo -*-. There is a page on the Octave wiki with tips on how to write nice help text including a short section on Texinfo syntax (the actual Texinfo manual can be a bit overwhelming).
In addition to the help text, the generate_html package also identifies %!demo blocks and generates a section with the demo code and output it generates, including figures.
The best way to see how help text and demo blocks work in Octave is to check the source (as #Andy pointed out in the comments). For example, see the source for inpolygon (scroll to the bottom to find the %!demo blocks, right before %!test and %!error). The generate_html package generates this page (note the Demonstration blocks).
This is a "multiple questions in one" question, making lots of assumptions in between, so let's address those first:
1. I'll start by the question in the comment, since that's the easiest: Matlab's publisher is not a code documentation tool. It's a "make a quick report that includes both text and code to show at your supervisor meeting or write a quick point in a blog" tool. So the link you point to is simply irrelevant in this case, since that talks about documentation for matlab code.
2. The fact that matlab's publisher also "works for functions", particularly given the first point here, should be considered to be more of a bug than a feature, or at most as a trivial undocumented extension. If you look at the matlab documentation for the publish command, you'll see it expects a filename, not a function with arguments, and the documentation only talks about scripts and makes no mention of 'function' compatibility.
3. Furthermore, even if you did want to use publisher as a "documentation tool", this is counterintuitive for functions in this format, since you need to provide arguments in order for publisher to work (which will not be shown in the actual report), you'll need a modified version that displays intermediate calculations (as opposed to your normal version which presumably does not), and the function just spews an ugly ans= blabla at the end of the report. If your end goal is documentation, it might be best to write a bespoke script for this anyway, showing proper usage and examples, like matlab does in its actual documentation.
Having said all that, yes, there is a 'cheat' you can do to include a function's code in your published report, which is that, in octave (also matlab since R2016b), functions can be defined locally. A .m file that only contains function definitions is interpreted as a function file, but if there are other non-function-declaration instructions preceding the function definitions (other than comments), then it is seen as a script. So if you publish this script, you effectively get a published report with function code in it:
%% Adding function
% This function takes an input and adds 5 to it.
%% Example inputs
In = 10;
%% The function itself
% Marvel at its beauty!
function Out = myfun(In)
%% Here is where the addition takes place.
% It is a beautiful addition
Out = In + 5;
end
%% Example use
Out = myfun(In)
(If you're not happy about having to create a 'wrapper script' manually, you can always create your own wrapper function that does this automatically).
However, both the matlab and octave publishers are limited tools by design. Like I said earlier, it's more of a "quick report to show numbers and plots to your supervisor" tool, rather than a "make nice documentation or professional reports" tool. Instead, I would invest in a nice automated latex workflow, and have a look at code formatting tools for displaying code, and simply wrap that code in a script that produces output to a file that you can then import into latex. It may sound like more work, but it's a lot more flexible and robust in the long term, particularly since the formatting commands can be very quirky as well as limited.
I'm having troubles with my code in Matlab. I want to get the mean value of all the elements in the second column in a file, but for some reason the code does not include the last line.
My file looks like this:
And my code looks like this:
As you might already understand, my code gets the mean value of all numbers except the last one for Italy.
Any suggestions on how to proceed would be highly appreciated.
It's actually suggested by Mathworks to not use feof with fgetl loops, but to instead check whether the output is with ischar. Simply replace ~feof(fid) with ischar(line).
A side note: line is also a MATLAB function, by using it as a variable name you are shadowing the function. While it is not critical here, you should try to avoid doing this. If you try to use line the function or another function which calls line while you have a variable line in the workspace, you'll likely get an error. This is why you'll see the examples in the help use things like tline as variable names instead.
You should put
line=fgetl(fid)
to the top in the while loop.
I have a lot of if statements throughout my code and was wondering if there is anyway in Matlab to see which if statements are being used when I run my code. I know I could put variables throughout my code and see which ones are being triggered, but I was wondering if there is an easier way. Maybe a built in MATLAB function or something.
Thanks
Type profile viewer in the command line of matlab and execute your code from there. There you can see in the profile report how many times each line is called as well as how long it takes executing the line of code.
More information:
http://www.mathworks.nl/help/matlab/ref/profile.html
http://www.mathworks.nl/help/matlab/matlab_prog/profiling-for-improving-performance.html
To answer precesely to your question, there is a command to log every line the execution is going through. And if you're familiar with unix-like platform, it is the same command: echo. See the Matlab help of echo to see how you can use it. For example, echo on all sets echoing on for all function files.
Besides that, I advise you two things better than analysing the output of echoing a whole script:
look at every warning in the code editor, and apply meaningful corrections.
use the profiler of matlab, as stated in the answer from EJG89, it is indeed a powerful tool!