Plotting Error in Matlab - matlab

Having an issue with printing my matlab plots into a PDF. After researching for a solution for several hours I have been unable to find a solution. I keep getting the same error message. I appreciate your help.
x = [2 4 7 2 4 5 2 5 1 4];
fig=plot(x);
print(fig,'-dpdf')
Error using
checkArgsForHandleToPrint>LocalCheckHandles
(line 88)
MATLAB Graphics handle must be a figure.
Error in checkArgsForHandleToPrint (line 30)
Error in print>LocalCreatePrintJob (line 336)
handles = checkArgsForHandleToPrint(0,
varargin{:});
Error in print (line 153)
[pj, inputargs] =
LocalCreatePrintJob(varargin{:});

plot returns the handle to a graphics object of type 'Line' which is at least two levels below 'Figure'. But the print function expects a figure (as the error message says).
Set fig=gcf instead of setting it to the output of plot(). That stands for "get current figure". Alternatively, you can take advantage of the convention that, at least by default, figure handles are equal to the integer numbers that you see in the figure title bars—so if you want to print figure 1, you could say print(1, '-dpdf')

Related

Invalid setting for parameter 'Gain'

I am trying to create a simple controller, but keep receiving an error for the gain block stating that there is an undefined variable 'u'. I do not understand Simulink well and I am just trying to replicate an old homework problem right now. The code I have here was provided as a solution, but I still receive the error when I try to run it. Any insights as to what might be going on?
I= 10; Wl= 5; k= 2; J= 1;
%set initial conditions
thetaIC= 0; phiIC= 0; x0= zeros(4,1);
%fix theta= 0, check output
[xe, ue]= trim('Ex3_System',x0,0,x0,1)
[A,B,~,~]= linmod('Ex3_System', xe, ue)
%choose your desired poles
p= linspace(-1.2,-1.5,4)
%recall the minus sign
K= -acker(A,B,p)
%perturb initial condition
thetaIC= deg2rad(5);
sim('Ex3_Controller');
Invalid setting in 'Ex3_Controller/Gain' for parameter 'Gain'. Caused
by:
Error using hw12 (line 57)
Error evaluating parameter 'Gain' in 'Ex3_Controller/Gain'
Error using hw12 (line 57)
Undefined function or variable 'u'.
Error using hw12 (line 57)
Variable 'u' does not exist.
Suggested Actions:
• Load a file into 'Base Workspace'. - Fix
• Create a new variable. - Fix
Update: After removing the u term from the gain block, I received a different error:
Error using hw12 (line 57)
Error in port widths or dimensions. Output port 1 of 'Ex3_Controller/Gain' is a one dimensional vector with 4 elements.
Error using hw12 (line 57)
Error in port widths or dimensions. Input port 1 of 'Ex3_Controller/Model1' is a one dimensional vector with 1 elements.
The Gain block takes the value of the input signal and multiplies it by the value of the gain. In your case the gain is K and that is all you need to put into the gain block (i.e. remove the *u, Simulink handles that for you.)
Once that is done, the dimension error you are getting is because your controller requires u to be a scalar, but you are feeding a 4 element vector into it. You need to change the appropriate parameter of the Gain block so that it does a matrix multiplication, taking the 4-by-1 matrix K and (matrix) multiplying it with the 4 element "out" signal to produce a scalar.

MatLab Power Law, Non-Positive Value Error

Hi I'm trying to fit a power model to my data using MatLab's fit function
fo = fit(log2(x(:)),log2(y(:)),'power1');
plot(fo,'g'), hold on
However when I run this I get the error
Error using fit>iFit (line 282)
Cannot fit Power functions to data where X has nonpositive values.
Error in fit (line 108)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in CurvedPowerLaw (line 20)
fo = fit(log2(x(:)),log2(y(:)),'power1');
When looking at my data and checking if any are less than 1, nothing is displayed
x(count_1)=M(i,1);
y(count_1)=M(i,2);
count_1= count_1+1;
if(M(i,2)<1)
display(M(i,1))
end;
M is a csv file with two columns. I also re ran the code for
if(M(i,1)<1)
and nothing was displayed. Checking manually and nothing seemed to be below 1 either.
i is just the line in the file that is being parsed. The file looks like
344,17
345,13
346,13
347,16
340,12
M(i,1) will result in returning one of the >300 numbers and M(i,2) will return ~10 value
Any help would be much appreciated!!
Thanks
While all values that were parsed in were >0 when scaling them by log2 that's where the 0 values started appearing. A quick fix was to add 1 to each value when parsing them in.

MatLab GUI Troubles: Calculating an Integral

I have created a matlab GUI in which a user selects a variable to integrate with respect to, inputs the equation, and lower and upper limits. When my code goes to calculate the integral on pushbutton Callback, I get an error I don't understand.
This is the line of code causing the error:
i1 = int( eval(get(handles.edit1,'string')),
(handles.respectvar),
get(handles.edit3),
get(handles.edit2)
);
%respactvar is the user-selected variable, and edit3 and edit2 are the lower and upper limits.
And this is the error message:
Error using sym>tomupad (line 1135)
Conversion to 'sym' from 'struct' is not possible.
Error in sym (line 151)
S.s = tomupad(x);
Error in sym/int (line 142)
b = sym(b);
Error in projectCALC>pushbutton1_Callback (line 376)
i1=int(eval(get(handles.edit1,'string')),(handles.respectvar),get(handles.edit3),get(handles.edit2));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in projectCALC (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in #(hObject,eventdata)projectCALC('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
Thank you!
There are multiple problems with your code.
The first problem, as #excaza noted, is that you forgot to get the string property of the last two edit boxes, while you did this for the first one.
The second problem is that eval is very inefficient, and, in your case, very unsafe. What if somebody wrote a system() call into the editbox, erasing your hard drive?
The third problem is that even with eval or str2func, int requires its first argument to be a symbolic expression. This is a good thing: you don't actually have to use eval, you just have to convert your first string to a sym.
The fourth problem is that handles.respectvar also seems to be a GUI object, so you might need to get() some property for it in order to be able to use it as an integration variable.
Assuming that my suspicion is correct, and your edit1 contains a string like '3*x+y', edit2 and edit3 are limits such as '1' and '3', and handles.respectvar has a property that evaluates to 'x', then you should be calling
formula = sym(get(handles.edit1,'string'));
variable = handles.respectvar; %// more likely: get(handles.respectvar,...)
lim1 = str2double(get(handles.edit3,'string'));
lim2 = str2double(get(handles.edit2,'string'));
i1 = int(formula,variable,lim1,lim2);
A few notes:
Don't be afraid of using temporary variables where it helps in readability.
I converted the formula from sym to string, otherwise you get a warning or an error (depending on MATLAB version)
I left the order of lim1 <-> edit3, lim2 <-> edit2. Make sure this is indeed what you want, and orders are not reversed.
I converted the integration limits to double, but it seems that sym/int will also accept string input as the limit variables, so this is mostly for clarity.

MATLAB newbie: error using sprintf. Function is not defined for 'matlab.graphics.GraphicsPlaceholder' inputs

Today I tried to run a MATLAB m-file that someone gave me. It works fine for him, but I encountered a warning and errors (below). I am using a Mac with OS X Yosemite (10.10.5) and a new version of MATLAB (R2015b). The person who prepared the m-file would have used an older one and Windows.
From a blog at Mathworks and posts online/here, the error seems to be due to a MATLAB update--a change from using numeric values to point towards graphics objects to using objects themselves. I understand this in theory, but don't know how to fix my code (it has been years since I used MATLAB regularly, so I'm pretty lost).
Warning: Text handle output is not supported when a contour handle
argument is supplied and label placement is automatic.
In clabel (line 214)
In control_volume_20150706>plot_xxx (line 733)
In control_volume_20150706 (line 104)
In run (line 96)
Error using sprintf
Function is not defined for
'matlab.graphics.GraphicsPlaceholder' inputs.
Error in control_volume_20150706>plot_xxx (line 734)
sprintf('%10.4f \n',text_handle);
Error in control_volume_20150706 (line 104) plot_xxx (nr,
xwidevec, yhighvec, omegamat, psimat, umat, vmat, ...
Error in run (line 96) evalin('caller', [script ';']);
This is what the code looks like in the vicinity of line 733:
Line 731 contourlevels = omegamat(1, :) ;
Line 732 [C,h] = contour(X,
Y, flipud(omegamat), contourlevels, 'LineWidth', 2 );
Line 733
text_handle = clabel(C,h);
Line 734 sprintf('%10.4f \n',text_handle);
I would be very grateful for any hints about how to deal with this.
If h is a handle which refers to some object, then in R2015a and later this is the object itself, while in previous versions it's a double which points to an object (as you noted in the question). You can use h.double in R2015a and later to get what would previously be h. E.g., pre-R2015a h = figure(999) would set h to 999, a double; with R2015a and later h is an object and h.double is 999.

MATLAB: findpeaks function

I'm using MATLAB 2013a and trying to find peak points of my data. When I tried code example given in
Find Peaks with Minimum Separation
I am getting the following error:
Error using uddpvparse (line 122)
Invalid Parameter/Value pairs.
Error in findpeaks>parse_inputs (line 84)
hopts = uddpvparse('dspopts.findpeaks',varargin{:});
Error in findpeaks (line 59)
[X,Ph,Pd,Th,Np,Str,infIdx] = parse_inputs(X,varargin{:});
I tried simple x and y vectors and got the same error. What can be the problem?
I have the same problem as you (R2013a on OSX) with the example by the Mathworks. For some reason it seems we can't use findpeaks with the x-and y-data as input arguments, We need to call the function with the y data and use the [peaks,locations] output to get the peaks/plot them.
It looks like in R2014b they changed some stuff about findpeaks that does not work with older versions...like calling the function with not output argument in R2014b plots the data/peaks without any additional step...but it does not for earlier versions.
Anyhow here is a way to workaround the problem. Call findpeaks with a single input argument (y data that is, you can use property/value pairs as well) and use the indices (locations) to show the peaks:
clc
clear
load sunspot.dat
year = sunspot(:,1);
avSpots = sunspot(:,2);
[peaks, locations] = findpeaks(avSpots)
plot(year,avSpots)
hold on
scatter(year(locations),avSpots(locations),40,'filled')
hold off
Output:
It might be worthwhile to contact The Mathworks about this. Hope that helps!