Display code in Command Window like 'help' in Matlab - matlab

I am looking for a way to display the code of a function (unformatted) in the command window. In this way I want to easily check what the expected input arguments are, the help text and perhaps some part of the code.
When writing
help functionname
I only get the help text
Is there a way to get the complete code?

Solution for stantard (non built-in) functions
What you want can be done as
type functionname
Example:
>> type mean
function y = mean(x,dim,flag,flag2)
%MEAN Average or mean value.
% S = MEAN(X) is the mean value of the elements in X if X is a vector.
% For matrices, S is a row vector containing the mean value of each
% column.
···
Work-around for built-in functions
Built-in functions don't have Matlab code; they are directly part of the interpreter. For those functions the above method doesn't work. For example:
>> type find
'find' is a built-in function.
However, usually there is still a function file, which consists of comments only. You can open it with
open find
This will open the file find.m in the Matlab editor, which contains:
%FIND Find indices of nonzero elements.
% I = FIND(X) returns the linear indices corresponding to
% the nonzero entries of the array X. X may be a logical expression.
% Use IND2SUB(SIZE(X),I) to calculate multiple subscripts from
···

Related

Simulink: Syntax error

I have a syntax error in the following equation:
((Cs+Cf)*u(1)/Cf-Cs*u(2)/Cf)*(1/(1+(Cs+Cf)/(Cf*(10^(u0/20)))))*(1-exp(-(pi*f1*(10^9)/(fs*10^6))*(Cf/(Cs+Cf)+1/(10^(u0/20)))))
Uppercase/lowercase checked, they are ok. What could be the problem?
As mentioned in the documentation of Interpreted MATLAB Function,
The Interpreted MATLAB Function block accepts one real or complex
input of type double ...
Here you're using multiple inputs which are Cs, Cf, u0, u, f1 and fs.
How to solve it?
Solution-1: Using Interpreted MATLAB Function block
One way to deal with this problem would be to concatenate all the input matrices into a single matrix and use its indices to represent each value in the equation.
e.g; if you have:
u=[1 5]; u0=5; Cs=1; Cf=1; f1=1; fs=20;
Concatenate them into a single matrix in your workspace. Something like the following would do:
new=[u, u0, Cs, Cf, f1, fs];
%It could be different depending on the dimensions of these
%variables that you actually have
then use the following equation according to the indices of new in the Interpreted MATLAB Function block:
((new(4)+new(5))*u(1)/new(5)-new(4)*u(2)/new(5))*(1/(1+(new(4)+new(5))/(new(5)*(10^(new(3)/20)))))*(1-exp(-(pi*new(6)*(10^9)/(new(7)*10^6))*(new(5)/(new(4)+new(5))+1/(10^(new(3)/20)))))
Solution-2: Using MATLAB Function block
You can also use the MATLAB Function block in which you can use multiple inputs. For your case, write the following code in it:
function y = foo(u,u0,Cs,Cf,f1,fs)
y = ((Cs+Cf)*u(1)/Cf-Cs*u(2)/Cf)*(1/(1+(Cs+Cf)/(Cf*(10^(u0/20)))))* ...
(1-exp(-(pi*f1*(10^9)/(fs*10^6))*(Cf/(Cs+Cf)+1/(10^(u0/20)))));
and connect Constant blocks with its inputs and give the values of the constants equal to the respective variables that you want to use.

Create a function handle within a structure, given some coefficients

I have a vector of coefficients available, which are the coefficients of an interpolating polynomial. I want to create a field in a structure as follows:
p.val =#(y) polynomial
Here polynomial is the polynomial with my coefficients in the indeterminate y. I'm not sure how to do this. The field has to contain a function handle like this.
The user has to be able to call this as follows:
pval = p.val(y)
where, y can be a vector of any length, supplied by the user (which contains values where the polynomial needs to be evaluated). So, this output has to be a column vector, such that every cell contains the specific function value.
Any help on how this can be done is appreciated!
I am not sure what function you want to implement, but the length of the vector you are including does not matter.
example.a = 3; % Any variable
example.b = #(s) mean(s);
The function mean here can be anything from a built-in function to a custom function you just made.
You can call it by:
result = example.b(rand(1000,1);

Plotting using scalar values. (vector/matrix/array input arguments are not accepted by code.)

I'm having a bit of trouble trying to make a 2D plot of a function depending on only one variable. Cutting a long story short, the function can only accept scalar values; it will not accept vectors. Hence it is not possible to use, for example, plot(vector, function(vector)), for a range of independent values vector. I've tried to use loop also, but my knowledge is limited, and it hasn't worked in any case.
To summarise: I want to plot function(x) vs x, however function may only have a scalar input, so taking x=-10:1:10 and then plotting it against function wouldn't work.
Can anybody point me in the right direction?
vector = -10:10 % set up your vector
output = zeros(size(vector); % initialise the output
for ii = 1:numel(vector)% loop over all elements of your vector
output(ii) = function(vector(ii)); % feed the function a single element at a time
end
plot(vector,output) % Now you can plot the two vectors

how to create a function to calculate hyperbolic sine of values

I am using the matlab 2007b and getting in a problem i want to make a function that takes input argument from the user and return sine hperbolic of that value.Sine hyperbolic should be define by that formula sinh(x)=(e^x-e^-x)/2
I am using that code while getting error
function sinh=sinhx(x)
% take the input value of x
a=exp(x)
% assinge exp(x) to a
b=exp(-1*x)
%assinge exp(-x) to variable b
c=a-b
%getting difference of these two variables
d=c/2
% dividing by 2 to get sinhx
end
kindly guide me how can I make this function or correct this code..thanks in advance for your assistance
The simpler the better: you should use the built-in sinh function.
Otherwise, in your function you don't define the output variable sinh, hence the error.
Best

Using a Function handle in Matlab

I would like to use knnimpute to fill some missing values in my dataset. Thing is, I would like to use my own distance function, instead of the typical ones (Euclidean, Manhattan...).
For what I've read, knnimpute allows me to use a function handle, that calculates the distance according to Heterogeneous Euclidean-Overlap Metric (HEOM)
I've implemented this function as a regular function, but not as a handle function. So, I cannot use the distance matrix from my "normal" function, because this has to be done inside knnimpute, somehow, as a handler...
I'm confuse, can someone help me understand what I need to do?
As long as your implementation of a distance function has the same signature as the standard distance functions, then you should be able to easily pass your function in.
From the knnimpute documentation (matlab knnimpute) it states that you can pass "A handle to a distance function, specified using #, for example, #distfun." It then refers the reader to the pdist function which provides more details (matlab pdist) about the custom distance function:
A distance function specified using #:
D = pdist(X,#distfun)
A distance function must be of form
d2 = distfun(XI,XJ)
taking as arguments a 1-by-n vector XI, corresponding to a single row of X, and an m2-by-n matrix XJ, corresponding to multiple rows of X. distfun must accept a matrix XJ with an arbitrary number of rows. distfun must return an m2-by-1 vector of distances d2, whose kth element is the distance between XI and XJ(k,:).
So as long as your distance function, as defined in your *.m file matches this signature and so can support these inputs, then there shouldn't be any problems.
Suppose that your distance function is in the mydistFunc.m file, and it's signature matches the above requirements, then all you should need to do is:
% call knnimpute with the data and your function
knnimpute(inputData,'Distance',#mydistFunc);