# Doug's MATLAB Video Tutorials

## Solution to MATLAB Graphics challenge for new users (1/2)

In last week’s post, I introduced a graphics challenge that I use when I teach a MATLAB graphics class to new technical support engineers at MathWorks. This week, I show the first part of the solution. This video shows how we draw one curve with two colors and how we draw a horizontal line across the plot. This week’s video makes use of concepts I’ve introduced in previous videos: . Next time we will show how to move the line and update the plot.
Part two

### 6 Responses to “Solution to MATLAB Graphics challenge for new users (1/2)”

1. Paulo Silva replied on :

Very nice tutorial, I was wondering how you were running the function without calling it from another script or workspace until you explained the Edit Run Configuration part, that’s something I never used before, keep up the good work Doug.

2. Matt Fig replied on :

Hello Doug,

This topic inspired me to develop a new FEX submission, LINECMENU. Thanks for the inspiration!

3. Peter replied on :

The “Edit run configuration” is new to me as well and sounds like I will use it a lot — starting now :-)
Thank you!

4. dhull replied on :

@Everyone,

Glad the advanced users are learning something from these videos also. The File Exchange file looks good. You even did the interpolation near the line. I cover that in my language class, it is kind of fiddly, and not really the point of this exercise, so I only assign that to the people that get done early! :)

5. Pezhman replied on :

“Edit Run Configuration” is awesome!

One thing about the code. I did this part, but I was getting “bad” graphs whenever x was too coarse or I had more than one period. Here is my code:

function h = dualColorPlot(x, y, lev)
Ind_LowY = find(y=lev);

LowY = y(Ind_LowY);
LowX = x(Ind_LowY);
HighY = y(Ind_HighY);
HighX = x(Ind_HighY);

h = plot(LowX, LowY, 'b', HighX, HighY, 'r')


I think NaN does the trick. Thank you Doug!

6. Jose Sanabria replied on :

Thanks Doug. NaN does the trick indeed in your code. Whether we are needing to represent a dual color graph with other colors than the MATLAB defaults, I suggest this modified version:

function createDualColorPlot(x, y, lev)
[yHigh, yLow] = FilterY(y,lev);
cox = [0.58 0.39 0.39];
coz = [0.23 0.44 0.34];
plot(x,yHigh,'Color',cox,'LineWidth',2);
hold on
plot(x,yLow,'Color',coz,'LineWidth',2);
hold off
xlimits = xlim;
line(xlimits,[lev lev],'Color','k');


The emphasis in the lines can be useful to give prominence some pieces of the graph.

 Name (required) E-mail (required, will not be published) Website (optional) Spam protection (required): What is 2 + 5 ?

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>


If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

These postings are the author's and don't necessarily represent the opinions of MathWorks.