0
votes

I have written a function of the form

function myplot(x,y)
plot(x,y)
end

This function creates a plot for given values of x and y. The actual function is more complex, but it does not serve the purpose of the question to include its content here. The main point follows.

I have tried to run the following script:

x = [1:0.01:10]
y = [1:0.01:10]
figure
plot(sin([1:0.01:10]))
hold on
myplot(x,y)

The intent here is to plot 2 sets of data on the same graph. The first set of data is generated by Matlab's native plot command while the second set of data is generated by the user custom myplot function (in this case what should be a straight line). The script above wont do it....

How to get Matlab to include both sets of data on the same plot?

2
Be sure of your plot limits. You might be plotting out of viewing range from one plot to the next. Otherwise, that basic example works for me. - Morc

2 Answers

0
votes

your script plots them both but with different x values. if you don't specify x input in plot it uses 1:length(y), while your myplot function does specify x values (which in your case are 10-fold smaller).

just do: plot(x,sin([1:0.01:10])) instead of plot(sin([1:0.01:10]))

0
votes

You could save the current axes (on which you create the first plot) in a variable and pass it as an argument to your function to be sure it gets plot on the same axes no matter what happens elsewhere in your code.

So your main code could look like this:

x = [1:0.01:10];
y = [1:0.01:10];
figure
plot(sin([1:0.01:10]))
hold on
%// Save axes in variable
CurrentAxes = gca;
%// Pass it as argument to function
myplot(x,y,CurrentAxes)

and the function:

function myplot(x,y,hAxes)

plot(hAxes,x,y);

end