MATLAB Answers

0

Ploting problems with handle, maybe I have changes in this function.

Asked by Francisco Molina on 19 Oct 2019 at 19:12
Latest activity Answered by Steven Lord
on 20 Oct 2019 at 0:08
I have a problem, when I try to plot any function, for Example this:
%pendulum1.m Solves y"+w^2*sin(y), y=theta, y0=10 degrees, y'(0)=0.
close all;
clear all;
clc;
g=9.81;
L=1;%SI standard units
w=sqrt(g/L); %rad/s
f=w/2/pi; % Hz
T=1/f; % period in seconds
tspan=[0 2]; y0=[85 0];%degrees
y0=(pi/180)*y0;%radians
pendulum= @(t,y) [y(2); -w^2*sin(y(1))];
[t,y] = ode45(pendulum,tspan, y0);
y=y*180/pi;%degrees
plot(t,y(:,1),'lineWidth',2.5);
xlabel('t'); ylabel('y_{1}(t), degrees');title('\theta (t)');grid
figure;
plot(y(:,1),y(:,2),'linewidth',2); xlabel('\theta (t)');grid on;
ylabel('d \theta / dt (t)');
I have this warnings and errors:
Undefined operator '/' for input arguments of type 'meta.class'.
Error in handle (line 3)
X = [y ? width/2, y+width/2, y+width/2, y ? width/2];
Error in claNotify
Error in cla (line 42)
claNotify(ax,extra{:});
Error in newplot>ObserveAxesNextPlot (line 152)
cla(ax, 'reset', hsave);
Error in newplot (line 93)
ax = ObserveAxesNextPlot(ax, hsave);
Error in matlab.graphics.internal.newplotwrapper (line 12)
axReturn = newplot(varargin{:});
Error in pendulolibro (line 15)
plot(t,y(:,1),'lineWidth',2.5);
The function Handle has the next code:
function handleip...
= drawBase(y, width, height, gap, handle, mode)
X = [y ? width/2, y+width/2, y+width/2, y ? width/2];
Y = [gap, gap, gap+height, gap+height];
if isempty(handle),
handleip = fill(X,Y,'m','EraseMode', mode);
else
set(handle,'XData',X,'YData',Y);
end
I need help and I hope someone can help me with this problem. Regards.

  1 Comment

I just edited the question so that your code is formatted. Otherwise it's difficult to see your comments between blocks of code. Please remember to format your code in the future.

Sign in to comment.

Products

3 Answers

Answer by Adam Danz
on 19 Oct 2019 at 19:23
Edited by Adam Danz
on 19 Oct 2019 at 21:16

From the documentation:
________________________________________________
?
Name: Question mark
Uses: Metaclass for MATLAB class
Description: The question mark retrieves the meta.class object for a particular class name. The ? operator works only with a class name, not an object.
Examples
Retrieve the meta.class object for class inputParser:
?inputParser
More Information
____________________________________________________
In your code, this line below is not proper Matlab syntax.
% v v
X = [y ? width/2, y+width/2, y+width/2, y ? width/2];

  0 Comments

Sign in to comment.


Answer by Star Strider
on 19 Oct 2019 at 20:50

Although I usually suggest that images of code are inappropriate, that might be a way to figure out what is wrong with this line:
X = [y ? width/2, y+width/2, y+width/2, y ? width/2];
since it is likely that the ‘?’ are actually Unicode characters that the MATLAB editor cannot deal with, and so replaces them with ‘?’. You need to use ASCII code operators only with MATLAB.

  0 Comments

Sign in to comment.


Answer by Steven Lord
on 20 Oct 2019 at 0:08

Adam Danz and Star Strider have correctly pointed out that you probably don't want to use the question mark operator. But there's another problem. Don't write a function named handle. In MATLAB, handle has a very special and heavily used meaning. Most if not all graphics objects are handle objects so if you shadow the built-in name few if any graphics functions are going to work.

  0 Comments

Sign in to comment.