How can I fix the error message in my matlab plotting program?

In summary: The plot function plots columns of Y versus columns of X. Thevectors X and Y must have the same length.The plot function plots Y versus the index of each value whenY is a real number. If Y is complex, plot(Y) is equivalent toplot(real(Y),imag(Y)). In all other uses of plot, the valuesof X and Y must be numeric.The plot function, like most other functions, acceptsnumerical inputs for X and Y. Therefore, instead of trying to plot 'b-', you should specify what exactly you want to plot. For example, you could plot a line connecting the points given by the vectors x and y by using plot(x,y). You could also
  • #1
mooncather
1
0
Okay so I am really new to matlab, I mean REALLY new. I am trying to write a program for one of my classes but I am having some trouble. I keep getting this error message:

? Error using ==> plot
Invalid first data argument

Error in ==> guessTraj at 89
plot('b-')


And here is my code:



Code:
function [output]= guessTraj(target_dis)

target_dis=input('distance :');

error = sqrt(target_dis);

real = isreal(error);

if real == 0 & isnumeric(target)== 0

output = 4

disp('Invalid Distance')

end



angle = input('Please enter the angle in degrees:');

radians = angle * (3.14159/180);

velocity = input('Please enter the velocity the ball is thrown:');

vel_error = sqrt(velocity);

vel_check = isreal(vel_error);



if vel_check == 0

disp('Invalid Velocity')

end



VY = velocity * sin(radians) ;

VX = velocity * cos(radians) ;

t = (2*velocity)/9.81 ;

x_dis = VX * t ;

y_dis = VY * t ;



H1 = target_dis + 2.5 ;

H2 = target_dis - 2.5 ;



x = 0:x_dis/50:x_dis;

plot(x,x_dis,target_dis,0,'mo')

xlabel ('Distance the ball Traveled')

ylabel ('Height of the ball')

legend ('Trajectory of Ball', 'Location', 'SouthEastOutside')

hold on



timeVector = 0:t/50:t;

plot('b-')

if x_dis <= H1 && x_dis >= H2

title('The ball hit the target')

output = 1;

disp('1. The ball hit the target!')



elseif x_dis < H1

title('The ball was underthrown')

under = target_dis-x_dis;

output = 2;

disp('2. The ball was underthrown')



elseif x_dis > H2

title('The ball was overthrown')

over = x_dis-target_dis;

output = 3;

disp('3. The ball was overthrown')

end

I'm not sure what I'm doing wrong. I want to plot the path of the object... What am I doing wrong?

Thanks so much!
Code:
 
Physics news on Phys.org
  • #2
plot('b-') is not a correct use of plot. It just tells MATLAB to plot using a blue line but doesn't tell it what it should plot. What exactly do you want to do with plot('b-') ?
 
  • #3
>> help plot
 

Related to How can I fix the error message in my matlab plotting program?

1. How do I plot a graph in MATLAB?

To plot a graph in MATLAB, use the plot function. This function takes in two vectors, the x-values and the y-values, as inputs. For example, if you have two vectors x = [1,2,3] and y = [4,5,6], you can plot them by using the command plot(x,y). This will create a simple line graph. You can also add labels, titles, and customize the appearance of the graph using additional commands.

2. How do I plot multiple lines on one graph in MATLAB?

To plot multiple lines on one graph in MATLAB, you can use the hold function. This function allows you to hold the current plot and add new plots to it. For example, if you have two sets of x and y values, x1, y1 and x2, y2, you can plot them on the same graph by using the commands plot(x1,y1) and hold on, followed by plot(x2,y2). This will plot both sets of data on the same graph.

3. How do I save a plot in MATLAB?

To save a plot in MATLAB, you can use the saveas function. This function takes in the handle of the figure you want to save and the name of the file you want to save it as. For example, if you have a figure with the handle fig1 and you want to save it as a PNG file named myplot.png, you can use the command saveas(fig1, 'myplot.png'). This will save the plot in the current directory.

4. How do I add a legend to my plot in MATLAB?

To add a legend to your plot in MATLAB, you can use the legend function. This function takes in a cell array of strings as inputs, which correspond to the labels of your plot. For example, if you have two lines on your plot labeled line 1 and line 2, you can use the command legend({'line 1', 'line 2'}) to add a legend to your plot. You can also customize the position and appearance of the legend using additional commands.

5. How do I change the color of a plot in MATLAB?

To change the color of a plot in MATLAB, you can use the plot function with an additional argument for the color. For example, if you want to plot a red line, you can use the command plot(x,y, 'r'). You can also specify the color using RGB values, or use the color function to change the color of an existing plot. Additionally, you can change the color of specific data points on a plot using the scatter function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
7K
Back
Top