Coming up with a Unique Matlab Graphics / Plotting Solution

2 views (last 30 days)
Hello,
I currently have Matlab plotting a series of lines, showing the line moving in time according to data I have collected. The plot simply pauses for a timestep between each new plot. I have provided an example in the screenshots below.
What I would like to do, however, is ultimately plot that line as the bottom edge of a box (see photoshopped example). I don't have a good idea of the route I would take to do this efficiently though. I could use some more experienced Matlab-user insight, even to point me in the right direction.
Thanks very much
Current MATLAB solution:
Photoshopped example of what I want:

Answers (2)

Star Strider
Star Strider on 11 Nov 2015
I’m not certain that I understand the question, but the patch function is the obvious answer. To use it, you have to give the function a circumscribed area in x and y to fill. This is generally not difficult, but can take some experimentation to get to work correctly.
  4 Comments
Mike Kovacs
Mike Kovacs on 11 Nov 2015
Sorry about that -- I will try and clarify the situation:
1. Ignore the left plot. It's just the right one that I'm concerned with --- the straight line (it's always straight).
2. The first 3 graphics show that straight line rotating. The line is plotted with a series of equally spaced points which (due to the nature of my data) are always going to produce such a straight line. The plot is produced with the following code:
% for i = 1:length(Steps)
subplot(1,2,1)
plot(Location(1,:), Sig(i,:))
xlim([-1250 1250]);
ylim([SigExt -1*SigExt]);
xlabel('Fiber Position From Center (mm)')
ylabel('Stress (MPa)')
grid on
subplot(1,2,2)
plot(Location(1,:), Eps(i,:))
xlim([-1250 1250]);
ylim([-1*EpsExt EpsExt]);
xlabel('Fiber Position From Center')
ylabel('Strain')
grid on
pause(dt)
end
3. The third graphic shows a large box above that line. I would like to produce a graphic similar to that, which "rides along" as the line rotates and shifts.
Steven Lord
Steven Lord on 11 Nov 2015
If you want to use a patch, the hgtransform approach described in this post on Mike's blog may be of interest and/or use to you.

Sign in to comment.


Mike Garrity
Mike Garrity on 11 Nov 2015
Edited: Mike Garrity on 11 Nov 2015
You've got some good ideas above, but I'd like to mention one thing that you're going to have to worry about. When you've got a rotated rectangle, you want the angles to all come out at 90 degrees. To do this, you're going need to worry about the DataAspectRatio.
Let's look at an example.
I wrote the following simple function:
function box_beside(x,y,w)
valong = [x(2)-x(1), y(2)-y(1)];
vperp = [-valong(2), valong(1)] * w / norm(valong);
fill([x(1), x(2), x(2)+vperp(1), x(1)+vperp(1)], ...
[y(1), y(2), y(2)+vperp(2), y(1)+vperp(2)], ...
[.75 .75 .75])
It takes a 1x2 X and a 1x2 Y (just like you'd give to line) and draws a rectangle of width w next to it. Let's try calling it:
rng default
x = randn(1,2);
y = randn(1,2);
line(x,y,'Color','r','LineWidth',3)
hold on
box_beside(x,y,1/10)
The result looks like this:
That doesn't look like a rectangle, does it? The problem is that our YLim is more than twice as big as our XLim. Rotating objects in a space with a non-uniform scale doesn't preserve angles.
The easiest way out of this is define the problem away. You can do this like so:
axis equal
The axis equal command simply tells MATLAB that you want the scale to be uniform. If you do this, you won't have to worry about this issue.
If you can't get away with axis equal, then you're going to have to compensate for the nonuniform scale. This involves looking at the DataAspectRatio and PlotBoxAspectRatio properties of the axes and compensating for them in that line where I computed vperp.
Does that make sense?

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!