How to plot data in MATLAB using custom plotting routines? - matlab

The code is as follows:
k=input('enter k');
i=0:1:k;
lambda=4;
cdf = exp(-lambda) .* ((lambda.^i)./ factorial(i));
plot(i,cdf);
When running this code, I am getting the following error:
Error using plot
Attempt to execute SCRIPT newplot as a function:
C:\Users\Sudhanshu ranjan\MATLAB\R2016a\toolbox\matlab\graphics\newplot.m
Error in Untitled (line 9)
plot(i,cdf);
My mynewplot.m file is as follows:
p = [0:0.00001:1]
a =p.* log(2.*(p))+(1-p).* log(2.*(1-p));
plot(p,a)
How do I resolve this error?

There's an inbuilt MATLAB function named newplot, which seems to be called when running a plot command. By defining a custom script named newplot.m, you're shadowing the functionality of MATLAB's newplot, thus the plot command tries to execute a FUNCTION newplot, but only finds your SCRIPT newplot.
You can resolve that issue by simply renaming your script, e.g. mynewplot.m.

Related

using ilaplace in conjunction with rltool

Using Matlab, I have the following code:
s = tf('s')
K=0.5;
H= 1/(s*(s+2));
Hcl=feedback(K*H,1)
ilaplace(Hcl)
rltool(H)
I want to get the inverse laplace transform of the unity closed-loop system.
rltool(H) generates automatically the unity closed-loop system. That's why I pass the open-loop system as an argument.
When I run this code, Matlab gives an error about ilaplace:
"Check for incorrect argument data type or missing argument in call to function 'ilaplace'."
Can someone help me how to use ilaplace and rltool concurrently
I have found a solution for this problem.
syms s;
K=1/2;
H= 1/(s*(s+2))
Hcl=simplify(K*H/(1+K*H))
P=poles(Hcl)
ilaplace(Hcl)
H = syms2tf(Hcl)
rltool(H)
ilaplace works with symbolic expressions and not with a transfer function. By converting the symbolic expression to a transfer function midway of the code, I can use both functions in the same code.
Notice I've added the function simplify and changed the beginning from s=tf('s') to syms s
The function syms2tf() can be found here

Plot error: Attempt to execute SCRIPT grid as a function: [duplicate]

I want to plot this in MATLAB:
x=[-2:0.01:4];
y=3.5.^(-0.5*x).*cos(6*x);
plot(x,y)
but it gives an error like this:
Attempt to execute SCRIPT plot as a function:
C:\Users\User\Downloads\Private\New folder (2)\Desktop\plot.m
Error in plot67 (line 3)
plot(x,y)
How do I resolve this?
Rename your script "plot.m" to something else. There's a name conflict. It thinks you're calling your own script and not the plot-function.
Please check in the current folder of MATLAB that you might have already created a file in the name plot.m
please delete or rename the file and try to plot.
then try to plot.

Problems with Embedded Functions within Simulink

I'm trying to simulate a very simple model using an embedded matlab function that takes the input and add's 10 to the value using a constant block that inputs into the matlab function, which then outputs to a display block.
As soon as I press simulate I get an abundance of errors. First I get a huge paragraph in orange text stating a warning out the solver 'variableStepDiscrete' instead of solver 'ode45'
Here is the remaining lines that are echo'd from the command prompt:
Code Directory :
"/Users/dazgti/Documents/MATLAB/slprj/_sfprj/embeddedFunction/_self/sfun/src"
Machine (#32): "embeddedFunction" Target : "sfun"
Chart "MATLAB Function" (#49):
.
"c2_embeddedFunction.h"
"c2_embeddedFunction.c"
"embeddedFunction_sfun.h"
"embeddedFunction_sfun.c"
"embeddedFunction_sfun_debug_macros.h"
Interface and Support files:
"embeddedFunction_sfun_registry.c"
Code generation failed Attempt to execute SCRIPT union as a function:
/Users/dazgti/Documents/MATLAB/union.m
I have a script file within my matlab directory called union.m, but I have no idea why its mentioning it.
function y = fcn(u)
%#codegen
x = u + 10;
y = x;
MATLAB Function block works by generating "C" code for the MATLAB code you entered in the block. In the process of generating code there could have been a call to union function in MATLAB from MATLAB Function block infrastructure. Since you have overridden the union function instead of the built-in function MATLAB might have attempted to call your script which caused the error. It is better to avoid naming your functions same as MATLAB built-in functions.

Error using parallel_function (matlabpool & parfor)

I want to read a large amount of files, process each of them and save the results for each of them in a .mat file. The processing of each file is independent from the others, so I'd like to try using parfor. I have written the following Matlab script file:
load filelist
obj = package.name.SomeObject();
matlabpool local 5
parfor i=1:length(filelist)
result = obj.compute(filelist{i});
[~, name, ~] = fileparts(filelist{i});
save(['~/path/' name], 'result');
end % file loop
matlabpool close
When I try to run it on my computer, a Matlab pool is intialized (connected to 5 workers), but then the following error message occurs:
Error using parallel_function (line 589)
Undefined function or variable "cleaner".
Error in readfiles (line 14)
parfor i=1:length(filelist)
Error in run (line 64)
evalin('caller', [script ';']);
Do you know where the problem could be?
I don't know why (there might be a connection to this issue), but the problem was solved by enclosing the code inside a function and calling that function (instead of calling the script file by run script.m). It also required creating a parsave function (see explanation here).

adftest function error in lagmatrix

Using the adftest function in MATLAB's econometrics toolbox, I'm receiving the following error:
>> [h1,pVal1] = adftest(y1,'model','ARD')
Error using lagmatrix (line 25)
lagmatrix: wrong # of input arguments
Error in adftest>runReg (line 705)
yLags = lagmatrix(y,0:(testLags+1));
Error in adftest (line 417)
testReg = runReg(i,y,testT,testLags,testModel,needRegOut);
y1 is a <41x1> vector of doubles.
Has anyone received this error or have any thoughts on what the issue is? I am using this code right out of the box so I'm not sure what is going on. I'd post to MATLAB's site, but it is down for maintenance.
This is either a bug in Matlab, in which case you should submit it on the Matlab support site. Before you do that, you should check that you don't have a function lagmatrix on your path that shadows the built-in function. Type
which lagmatrix
on the command line. If the path does not point to your Matlab installation, you should move lagmatrix off the Matlab search path.
Also note that y1 should not contain all NaN, or be otherwise degenerate, so you may want to check the function using the sample data as suggested in the help to be sure it's a bug and not just your data.
I had the same problem with this function. In my case, the problem was the function lagmatrix (older version) in my MATLAB path and the adftest function was the newest version. The soluction was delete the older version of lagmatrix.