Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 5: Plotting Styles Write a function rbgplot that will create a plot. The

ID: 656087 • Letter: P

Question

Problem 5: Plotting Styles Write a function rbgplot that will create a plot. The function has three inputs and no output. In the function body, define two X and Y vectors using the first input as their length. X and Y should contain random real numbers in a range defined by the second and the third input. For example, if the function inputs are 5, 6, and 7, the X and Yare vectors containing 5 random real numbers between 6 and 7. Then, ask the user for a color (red, blue or green) and for a plot style (circle, cross or square). Create a string pstr that contains the color and the plot style, so that the call to the plot function would be: plot (x, y, pstr) For example, if the user enters blue and o, the variable pstr would contain 'bo'. Plot y versus x and save the figure as _.jpg (for our example, the file name will be blue_o.jpg).

Explanation / Answer


x = [ 1 2 3 4 5];
y = [ 3 5 6 9 2];
plotcolor = input('What type of color do you want (r(red) b(blue) or g(green)): ','s');
plottype = input('What type of plot style would you like(cirlce(o) or star(*)): ','s');
pstr = sprintf('%s%s',plotcolor,plottype);
plot(x,y,pstr);

The plot function will try to evaluate 'pstr' as a string and not the variable if you use it in quotes. The values 'pstr' are not valid arguments in the color/linetype argument for plot. If you still are receiving errors, try to delete all the variables in your workspace and clean up your script file.