Add SPDX license information to .m Matlab files - matlab

What is the proper place to add SPDX license information to .m Matlab files?
In Matlab .m files are often written as functions with one (main) function per file.
Matlab interprets comments of functions as inline documentation.
If we simply write SPDX comments there, it will be rendered in the help information for the function.
https://de.mathworks.com/help/matlab/matlab_prog/add-help-for-your-program.html

Related

How do I keep the help-text when creating .p file?

I have a m.-file with a function inside using "new>function" and name the function "foo".
>> type foo.m
function [outputArg1,outputArg2] = foo(inputArg1,inputArg2)
%FOO Summary of this function goes here
% Detailed explanation goes here
outputArg1 = inputArg1;
outputArg2 = inputArg2;
end
>> help foo
foo Summary of this function goes here
Detailed explanation goes here
After creating a .p-file, however, the help text is not available anymore.
>> pcode foo
>> delete foo.m
>> help foo
foo is a function.
Can I obfuscate/protect my code, while still having help text available?
By design pcode() only contains parsed MATLAB code. This means that any comments in the code, including the help section, will not be available after obfuscation. The officially promoted way of including a help section after obfuscation is to write a separate .M-file with the same name containing the help section. Thus, save a file foo.m:
%%FOO does bar
%
% Inputs:
%
% More help text
% Blabla
Note that you don't have to actually call foo.p inside your foo.m function:
If both a .M and .P file exist, MATLAB will call the P-file for execution, so the .M file only needs to contain the help comments.
Converting .M files to .P files is just obfuscation not encryption or compilation. This means that the possibility to de-obfuscate exists. See protect your source code by The MathWorks (emphasis mine):
Deploy as P-code — Convert some or all of your source code files to a content-obscured form called a P-code file (from its .p file extension), and distribute your application code in this format. When MATLAB P-codes a file, the file is obfuscated not encrypted. While the content in a .p file is difficult to understand, it should not be considered secure. It is not recommended that you P-code files to protect your intellectual property.
As a side-note, something similar can be seen when calling the source code of built-in functions, e.g. edit sum shows nothing but the help for sum, not even a function keyword on top.

How to run several .m files by creating a master .m file

I have a series of .m files that plots Power spectral density, one third octave band levels, spectrograms etc. Each one of them is a seperate .m file (function file to be specific) that is executed by one main .m file which opens up a GUI that interactively lets us choose what we want and then it gives us the outputs accordingly. The thing I want to do now is create a small script that will entail to initiate to run these series of .m files and store the results so that I can use these results (probably by saving them as .mat files) to plot further results.
Any suggestions or ideas are welcome! Many thanks.
Further to the above, please find below details -
so can save the myOutput (referring to Wolfie's comment) as and when the function is being called within the main .m file? The thing with the GUI is it does RUN based on user's selection to analyse .wav files. suppose i choose one file then it does the analysis according to what i want and gives me outputs. suppose i want to do the analysis for batches of .wav files at a time, then it is taking lot of time. i wish to know if i could write another script to direct this main .m file to do the analysis of required things in batches and store the results so that in the end i can use all the stored results to plot as to what i need to plot. hope this is helpful. thanks all.
You can run MATLAB scripts from command line or code with the run function.
Suppose you have an *.m file stackoverflow_playground.m with some content you can let that script run by
run('stackoverflow_playground.m')
So one master script could contain several run statements after each other to run consecutively the desired scripts given their position in the code. This master script could then also include some save routines for the obtained results.

How do I call a c function from inside a mex file in Matlab?

I have a mexFunction defined in a .c file, written in the regular mex wrapper format. I would like to be able to call another function, written in C, from inside that first function. How can I do this? Do I need to create a regular .c file and just include it at the beginning of the first file? I would like to be able to pass variables from within the mex function to this secondary C function.
The documentation for mex has two subsections that describe how to build a MATLAB extension when the source code is spread over multiple source files. Mostly, all you need to do is:
mex mexname.c helper1.c helper2.o
The result is automatically named according to the first file passed in.
For more information, read the documentation sections "Build MEX-File from Multiple Source Files" and "Create and Link to Separate Object Files". There are also sections discussing using libraries.

How to see .p files' required input?

I have an .p file, this has to take some inputs and give me results, but I don't know which inputs I have to give to it. Does anyone know how can I learn it? after writing its name opening parenthesis is not working.
You cannot see the code of a Matlab p-code file.
p-code files exists so that one can share code in order that others can't look at it.
type on matlab command window;
help <p-code file name>
(if the corresponding .m has comments sections for its inputs and return values, or other documentations, you can probably see them)
You could probably get some idea for its inputs, from the error thrown just by typing the p-code file name.

what does "V1MCC4000MEC1000MCR1000x" mean in matlab's .m file?

I have a problem with a .m file that I'm not able to read:
it starts as
V1MCC4000MEC1000MCR1000x
and that follow with many unreadable character like
漜哹逓馎(S煈KPO嘀菏戓N缻k?軥慍┎嘳鏴敕?簛ei?梤>?2;瀱啿唱煩烮?闑XSぜ时鷍徍頰r+燏p赠髬 <笉rVw拹p9_?=江秡?v
?R?RJ崀戓繝欴
What's wrong this .m file?
How could I transform it to the normal one?
Googling the string V1MCC4000MEC1000MCR1000x is enlightening. You may have not a .m source file, but a compiled executable on your hands.
Here's what Walter Robertson had to say about it:
The reference to MCR there suggests to me that what you have is not a
.m file but instead the result of running mcc to "compile" .m files (I
believe the output of that is a .exe file.)
But without further research I would not rule out the possibility that
it is a matlab .ctf (Component Technology File) or .p (p-code) file.
If you do, indeed, have a compiled .m file, there is no way to decompile it back to the original .m source.