MATLAB won't plot my graph

3 次查看(过去 30 天)
Ian Connelly
Ian Connelly 2016-2-25
So I'm trying to get the following code to plot on a graph:
for x = -3.0:0.1:3.0
for y = -3.0:0.1:3.0
for z = power((sin(x.*y)),2)
figure
if (z < 0.1)
plot(x,y, 's', 'b')
hold
elseif (z >= 0.1 && z < 0.5)
plot(x,y, 's', 'c')
hold
elseif (z >= 0.5 && z < 0.75)
plot(x,y, 's', 'g')
hold
elseif (z >= 0.75 && z < 0.95)
plot(x,y, 's', 'r')
hold
else
plot(x,y, 's', 'k')
hold
end
end
end
end
And it keeps throwing "Error using plot Invalid first data argument" at me I'm not quite sure why, considering in the first iteration of the loop, x and y are only defined as one number, not a vector.
Any help would be appreciated.

回答(2 个)

Walter Roberson
Walter Roberson 2016-2-25
When you use two quoted strings like that, plot (at least in R2014a) tries to interpret the 's' as being the name of a property. If you are trying to use 's' as the marker shape and then a separate color, then you need to either combine the two into a single linespec like 'sb' or else you need to switch over to using a name/value pair like 's', 'color', 'b' or the fuller 'marker', 's', 'color', 'b'

Jason Allnutt
Jason Allnutt 2016-2-25
编辑:Jason Allnutt 2016-2-25
Also, you're "figure" command and the individual "hold" commands gave me trouble when I tested your code. In the sample I have provided I removed the "figure" call and moved a single "hold on" command to the beginning of the script.
Not sure if the outcome is what you expected but please run it and let me know.
hold on
for x = -3.0:0.1:3.0
for y = -3.0:0.1:3.0
for z = power((sin(x.*y)),2)
if (z < 0.1)
plot(x,y, 'sb')
elseif (z >= 0.1 && z < 0.5)
plot(x,y, 'sc')
elseif (z >= 0.5 && z < 0.75)
plot(x,y, 'sg')
elseif (z >= 0.75 && z < 0.95)
plot(x,y, 'sr')
else
plot(x,y, 'sk')
end
end
end
end

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by