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.
Related
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.
I want to use fig2texPS to export plots from MATLAB to LaTeX. I copied the the script to D:\Eigene Dokumente\MATLAB\fig2TexPS and added this folder as a path in MATLAB. Unfortunately, I receive the following error:
>> fig2texPS()
Undefined function 'find' for input arguments of type 'matlab.graphics.chart.primitive.Line'.
Error in fig2texPS (line 526)
lsh = double(find(handle(lshandles),'-class','graph2d.lineseries')); % find plots
PS: I use a version of the script which was updated in order to work with pdtlatex (http://pastebin.com/xK6KxxBT) that I found in the MATLAB Central File Exchange. The same error also occurs when I use the original script.
This might not be the answer you are looking for, but there is the marvelous matlab2tikz that does the job of conversion of figures quite nicely, which you could use as an alternative to fig2texPS.
I also use the function fig2texPS.m and it works very well. It might be an answer for your question to use an older version of Matlab. I am using 2013b and it works. While I have tried this function with Matlab 2015a, I was getting the same error message.
I hope my answer helps you with your problems.
I am trying to use an open source Matlab figure exporter called fig2texPS but I can't seem to get it to work, the documentation says I have to get the figure number of the figure I want to export then call
fig2tex(myFigureNumber)
but I don't know how to get the so-called "figure number".Does anyone have some experience with this project or someone who can understand this documentation guide me on how to use it?
UPDATE: Tried plotting a function then getting the figure number from the figure window but it still does not work.Can you please clarify a bit more on how I would get the "figure handle"?
x = 1:1:10;
y = 1:1:10;
plot(x,y);
fig2tex(1); //Undefined function 'fig2tex' for input arguments of type 'double'.
It's the figure handle, which you can get when you make the figure (myFigureNumber = figure;), or with gcf if the figure is active/current, or with other functions. However, most of the time you will see it right here:
EDIT: For your updated error message, the error is because MATLAB can't find fig2tex (should be fig2texPS). Find the path to fig2texPS.m and add it to your path with addpath C:\path\to\fig2texfolder where that file lives. Verify that it works with which fig2texPS.
Working on MATLAB 2008, I am trying to save all the images my scripts produce when invoking the "plot" function.
In order to achieve this, I have two possible solutions:
Either I write another function having the same parameters and perform a search/replace in the *.m sources
or I override the plot.m file so that I write the image into a specific directory when generated.
I did many searches and I am unable to find the plot.m source file. The only file I found is located in the toolbox directory and does not contain any code (except some commented documentation).
you can simply use the print command and save them into a directory that you can also make using the mkdir command.
Sample code
clc; close all; clear all;
x = 1:10;
y = x.^2;
plot(x,y)
if exist('plots','dir') ~= 7
mkdir('plots'); % make directory if it does not exist
end
print -dpdf ./plots/jawn.pdf
Read the print documentation, to learn how to print in other file formats
Also, I would not suggest overriding the plot command, and you will likely not be able to find the source code for plot.m because that is proprietary MATLAB code
I have a GUI(made using GUIDE) in which there is an axes on which I can draw. When I saved the gui, i have a .fig file and a .m file (whose names are start_gui.m and start_gui.fig). Now, I am trying to plot on these axes using an external M file, to which I have passed the GUI handles. This is as follows:
function cube_rotate(angle1,angle2,handles)
gcf=start_gui.fig; %this is the name of the gui.fig file in GUIDE
set(gcf,'CurrentAxes',handles.cube_axes)%this allows us to plot on the GUI
%plot something
end
handles.cube_axes is the name of the handle in the GUI created using guide. Inspite of passing the handles, it won't allow me to plot in the gui. It throws up an error saying:
??? Undefined variable "start_gui" or class "start_gui.fig".
start_gui.fig is the name of the GUI figure that was generated in GUIDE. How do i make it plot in the axes in start_gui.fig?
Thanks for all the help!
You've made a few errors. The first is referring to a file name without single quotes to denote a string. The second is trying to open an existing figure by assigning it as a variable named gcf. This will just give you a variable gcf which contains the string 'start_gui.fig'.
Instead, open the figure with this command:
fH = hgload('start_gui.fig');
% Then find/assign the axes handle (assuming you only have one axes in the figure):
aH = findobj(fH,'Type','axes');
% And finally plot to the axes:
plot(aH,0:.1:2*pi,sin(0:.1:2*pi));
On a secondary note, is there a reason you're not using the M-file generated by MATLAB to carry out this functionality? By using the auto-generated M-file, you'll be able to access the handles structure rather than using findobj.
The error you're getting is because of your second line: gcf=start_gui.fig;
It's looking for a variable named start_gui, which you don't have. (start_gui.fig is a filename, not a variable.)
To solve your plotting problem, take a look at this Mathworks support article.