I have never used Octave before this (I have used Matlab), but I installed GNU Octave because I wanted to use one of the repository/package that was written in GNU Octave. That repository has files with extension .csl, which are called within the .m files (main scripts) without their extension. For example, a file named foo.csl is called like a function foo() within the main script. However, when I run the main script (.m file that calls the .csl file as a function) it throws an error saying that the function foo() is undefined. The file foo.csl begins as following:
class foo
% Definition about the class foo
public x
public y
public z
I searched for .csl file extensions associated with GNU Octave, but I could not find anything helpful. I am using the latest version of GNU Octave on Windows 10.
I've had a look at your files.
The bad news is, as I've said in the comments, the .csl file is not valid matlab / octave code. This leads me to believe that one of the following might be happening:
The .csl file is "processed" elsewhere to produce an actual matlab / octave compatible class
The .csl file is simply a pseudo-code "specification", and the actual matlab / octave class is provided elsewhere, and you're supposed to 'load' it somehow.
This was part of an assignment, where whoever gave you this code was expected to convert the .csl file into appropriate matlab / octave code.
Whoever wrote this doesn't know matlab and this is just plain wrong code.
The good news is that this is very easy to translate into working code. Since your desired RecDomain "class" is essentially a simple class with exclusively public fields and no methods, it can be straightforwardly replaced by a simple struct. Meaning you can replace the entire RecDomain.csl file with the following:
%%% in file RecDomain.m
function Out = RecDomain (varargin)
%RecDomain() creates a domain with given parameters.
%RecDomain(d) creates a domain copy.
switch nargin
case 1 % a struct was given as input
Out = varargin{1};
case 3 % individual Dx, Dy, Dz arguments were given as input
Out.Dx = varargin{1};
Out.Dy = varargin{2};
Out.Dz = varargin{3};
otherwise
error('Wrong RecDomain constructor.\n');
endswitch
endfunction
and then your model1.m script will work as is.
PS. (obviously the above is oversimplified and has no input checking / assertions etc, but you get the picture).
If this was a contrived simple example and your actual .csl files are more complex, then you'll have to convert them into proper octave classes yourself based on that .csl "specification", which is beyond the scope of this answer. Octave provides some limited support for the new matlab object-oriented style using the classdef keyword if you'd like to try that, but for the most part octave implements object-orientation using matlab's old (pre-2008) style. See here for the respective official documentation entries: [matlab (new syntax)] / [octave (old syntax)]
Related
I have coded an algorithm using MATLAB R2019 script and i want it to be called in an System verilog file i.e The output generated by the matlab script is actually to be fed into the testbench written using SV. I dont want to use HDL coder tool as the algorithm is quite complex and re-coding it in SV/ C is quite difficult. I use synopsys VCS tool for compilation and elaboration.
My question is :
1. Is it possible that a MATLAB script to be called in testbench written in SV ? I've heard about DPI, but not much idea on it, or worked on it.
2. Can the output of the MATLAB script stored in a separate file, let's say for example a text file and i can call that file in my SV test bench.?
To answer your questions in order, it is indeed possible.
You need to do the following:
In SV, import a C function (extern DPI-C) that you will call as required. Say we call this callMatlabFn
In C, define an extern function called callMatlabFn. This will then actually call your matlab fn. Have a look here for calling matlab in C : https://www.mathworks.com/help/matlab/matlab_external/call-matlab-functions-from-c-1.html
Note, you could return data via DPI but that may have a different meaning. It may be best to return any data in SV via reference in an output arg to the imported fn.
Finally, text File I/O in SV is implemented via the following system tasks:
$fopen (file_name) ;
$fclose (file_name) ;
$fdisplay (arguments) ;
$fwrite (arguments) ;
$fstrobe (arguments) ;
$fmonitor (arguments) ;
$readmemb ("file", memory_identifier [,begin_address[,end_address]]) ;
$readmemh ("file", memory_identifier [,begin_address[,end_address]]) ;
In MATLAB, there are roughly 3 ways to define functions: non-comment-only .m files, .p files, and compiled code (e.g. DLL, MEX).
Knowing where a function is defined could be helpful in several cases, such as when a breaking change was introduced to some function outside our control, and we'd like to try to revert to an old version in the hopes of getting our code working again; or when trying to reverse-engineering some undisclosed algorithm.
The which function is usually very good at identifying function definitions and their locations (which works for .m, .p and MEX), but isn't very useful when it comes to shared library functions, where (at best) it points to a comment-only documentation file:
>> which _mcheck
built-in (undocumented)
>> which svd
built-in (D:\Program Files\MATLAB\R2019a\toolbox\matlab\matfun\svd)
If so, assuming a function found within a shared library is called during the execution of my code, how can I locate the specific file (DLL) that contains it?
It turns out that dbstop can be used for this. For example:
>> which svd
built-in (D:\Program Files\MATLAB\R2019a\toolbox\matlab\matfun\svd)
>> dbstop svd
Warning: Entering debug mode is only supported within running MATLAB code files.
Warning: MATLAB debugger can only stop in MATLAB code files, and "libmwmathlinalg>svd" is not a MATLAB code file.
Instead, the debugger will stop at the point right before "libmwmathlinalg>svd" is called.
From there's it's just a matter of finding a file called libmwmathlinalg (with the relevant extension) - which isn't a difficult task if your drive is indexed.
Short version of question
In recent versions of Matlab (I have seen it in R2014b and R2015a on Windows), when you type help foo you get a brief description of the function and its signatures. For example, typing help bsxfun produces something like this (only with better format):
This MATLAB function applies the element-by-element binary operation specified by the function handle fun to arrays A and B, with singleton expansion enabled.
C = bsxfun(fun,A,B)
Reference page for bsxfun
See also arrayfun, repmat
Other uses of bsxfun
distcomp/bsxfun
This is of course only a summary of the actual documentation. To get the full documentation you need to type doc foo. This opens the HTML help browser, which takes quite some time (at least on some computers).
Is there a way to get the full help in the command window (thus avoiding the help browser), as it used to be in older Matlab versions?
Long version of question
To look into this in more detail, I'll define "old" Matlab versions as those that don't have HTML help, and "new" versions as those that do. I also need to give each type of help a name, in order to refer to them:
FP (Full, Plain): full help in the form of plain text, shown in Matlab command window (old style).
SH (Summarized, HTML): summarized help in the form of HTML, shown in Matlab command window.
FH (Full, HTML): full help in the form of HTML, shown in the help browser.
As is well known, the text for FP help is contained in the first comment lines in the file defining the function. In new Matlab versions, functions may also have an associated HTML file. This file contains SH help in an HTML tag, and FH help in HTML code.
Possible behaviour is:
In old Matlab versions, help foo produced FP help.
In new Matlab versions, help foo produces SH help if foo has an associated HTML help file, and FP help if it doesn't.
In new Matlab versions, doc foo produces FH help if foo has an associated HTML help file. If it doesn't, FP help is shown in the help browser (without format).
So the problem is more properly phrased as: how to show FP help in new Matlab versions when foo has an associated HTML help file. The question is meaningful because
Most Matlab functions do have an associated HTML help file.
Most Matlab functions, even built-in functions (that have no m-code), have and m-file containing FP help.
An additional motivation is that in some cases the FP documentation contains features that don't appear in the FH documentation (see for example here).
Original answer (Matlab versions R2014b, R2015a)
Although the documentation doesn't tell, the help function in these Matlab versions supports zero, one or two output arguments. You can check this typing open help and looking at the function signature:
function [out, docTopic] = help(varargin)
In essence, help works internally as follows:
It creates an object called process, of class helpUtils.helpProcess, by calling the class constructor as:
process = helpUtils.helpProcess(nargout, nargin, varargin);
where nargout, argin and varargin are those with which help was called.
It runs the method process.getHelpText, which calls the undocumented, built-in function helpfunc, and as a result sets the property process.helpStr. This property contains the help string which is shown in the command window.
As it turns out, at least on Windows, depending on the value of nargout (which gets passed to the constructor helpUtils.helpProcess) the resulting help string will be FP or SH. Namely, it will be FP if nargout>0, and SH if nargout==0. You can check this by typing the following code (adapted from help.m) directly in the command window:
process = helpUtils.helpProcess(1, 1, {'bsxfun'});
process.getHelpText
process.helpStr
This will produce FP help. On the other hand, changing the first 1 (which corresponds to nargout in the actual call) into a 0,
process = helpUtils.helpProcess(0, 1, {'bsxfun'});
process.getHelpText
process.helpStr
will produce SH help.
I don't know why this is so, that is, how it works on a deeper level than this. All I know is that the getHelp method calls the undocumented helpfunc, which is at least involved in producing FP help.
So, to get FP help you need to call help with one or two output arguments. For example,
str = help('foo')
assigns the FP help text to variable str and displays it. Or you can use
disp(help('foo'))
which also has the effect of calling help with an (implicit) output argument.
To have this behaviour from the standard command help foo, you could define a help function to override Matlab's help, and place it in your Matlab document folder. This folder normally appears first in the path (or you can make sure it does by editing startup.m), and thus has precedence. The new function will essentially call Matlab's help with one output argument, and then display the resulting (FP) help text. In order to call the overriden function it is necessary to temporarily change to its folder:
function help(varargin)
if isempty(varargin)
varargin = {'help'}; %// `help` should be equivalent to `help help`
end
d = pwd; %// take note of current folder
cd(fullfile(matlabroot, 'toolbox', 'matlab', 'helptools')) %// folder where the
%// standard `help` function is
disp(help(varargin{1}));
cd(d) %// restore folder
So now, finally, help foo produces the old-style (FP) help.
Edit for Matlab version R2015b
In Matlab R2015b the behaviour seems to have changed for the better. Typing help foo no longer produces SH help. It's not exactly FP either. In fact it's better than that: it produces FH help but in the command Window, not in the browser. Or, equivalently, it produces FP help but with links and better formattting.
So no need to tweak anymore!
Edit for Matlab version R2018a
Matlab R2018a again gives SH help. The solution provided in this answer works (that is, produces FP help).
So back to tweaking!
A better way is to include the full path to the function when using the help command, then old style full help is displayed and the links also work, e.g. try:
help surf
help(fullfile(matlabroot, 'toolbox', 'matlab', 'graph3d', 'surf.m'))
I’ve just submitted an override help function based on this to MATLAB FEX:Full Command Line Help
I would like to optimize a function written in Matlab by converting the code to C\C++. The result should be callable from within matlab, as it is a small part of a larger matlab code.
For example, converting my function to C code wrapped in a .mex file would work.
I heard matlab coder package can help with that.
As I am unfamiliar with this package, what is the quickest way to achieve this?
If you have a license for MATLAB Coder, then, yes, that is the correct package to use. The function you're looking for is codegen. There are restrictions on what can be used in code generation: to see if your function meets those restrictions, add the tag %#codegen to the beginning of your function as shown below
function foo(bar) %#codegen
<your code here>
and open the function file in the MATLAB editor. The tag tells the editor to check that the code complies with the rules for code generation. Once the editor shows that your code complies with those rules, generating the mex file may be as easy as
>> codegen foo
which would generate a mex-file, foo_mex in the current folder. For your particular function you may need to use some of the optional arguments for codegen to generate the mex-file properly.
i have a problem to using a dll fortran in matlab.
i couldn't use a dll ,that is built by fortran, in matlab. i use "loadlibrary" instruction in matlab but the error is related to header files.
what is header files??
please give me more information to load a dll fortran in matlab and call it.
Rather than try to use a dll file directly I suggest you re-build it using Matlab's MEX functionality. Yes, a mex file is a dll and you can build dlls outside Matlab and use them successfully, it's a lot easier, for a beginner such as I guess you to be, to use MEX. One way in which it is easier is that, if you build a mex file, the system won't ask you for a header file which is, as you know, a rather foreign concept to a Fortran programmer. Another way in which MEX will make your life easier is that you can then call the function exposed by the dll directly from Matlab's command line, without loadlibrary.
Study the Matlab documentation on MEX files, pay particular attention to how to integrate Fortran this way.
Without seeing your header file and the command line you're using in MATLAB, it's hard to help you too much here. You might reference the documentation in MATLAB which request that you pass two arguments to loadlibrary, the second being the header file with function signatures. I am guessing you are not providing this second argument.
You need to provide a header file that defines each of the named functions in the Fortran DLL that you'll be calling. For instance, if your DLL contains a function named sum that sums two double precision variables, like:
function sum(a,b) result(sum)
real(kind=2), intent(in) :: a, b
real(kind=2) :: sum
sum = a + b
end function
Then your header will need to contain something like:
double sum(double*a, double*b);
But don't forget to decorate this with the name mangling specific to your Fortran compiler. For instance, if sum was in a module named foo, and you compiled with gfortran, then you will need something like:
double __foo_MOD_sum(double*a, double*b);
There are a lot of other cases, but that's the gist of it.