Plotting line between points

4 views (last 30 days)
Adaptine
Adaptine on 18 Nov 2016
Edited: Adaptine on 18 Nov 2016
Hello
I've been searching around a bit but can't find any similar problem/solution. Say I'm generating a matrix with following values:
phase =
0.0010 -90.0000
0.0457 0
0.1903 90.0000
1.9026 0
100.0000 0
What I'm looking to do is to plot a line between those points like this:
(0.0010, -90) to (0.0457, -90)
(0.0457, -90) to (0.0457, 0)
(0.0457, 0) to (0.1903, 0)
(0.1903, 0) to (0.1903, 90)
(0.1903, 90) to (1.9026, 90)
(1.9026, 90) to (1.9026, 0)
(1.9026, 0) to (100, 0)
How can I do this without too much fuzz? Basically some sort of a square wave with variable amplitude.
I got the result I wanted doing it like this, but I was wondering if there was another clever way of doing it?
j = 1; k = 2;
for i = 1:length(phase)*2-2
semilogx([phase(j,1) phase(k, 1)], [phase(k-1, 2) phase(j, 2)], '--r')
j = j + mod(i, 2); % Increment 1 when odd
k = k + ~mod(i, 2); % Increment 1 when even
end

Answers (2)

Adam
Adam on 18 Nov 2016
Edited: Adam on 18 Nov 2016
phaseX = [phase(:,1)'; [-1 phase(2:end-1,1)' -1] ];
phaseX( phaseX == -1 ) = [];
phaseY = [phase(1:end-1,2)'; [phase(1:end-1,2)'] ];
figure; semilogx( phaseX, phaseY(:), '--r' )
does the job I think. Whether it is 'clever' or not is debateable. There's probably a neater way to put the x values together that someone can probably come up with!

Adaptine
Adaptine on 18 Nov 2016
Edited: Adaptine on 18 Nov 2016
That's a neat way. Improved it abit:
phase = repelem(phase, 2, 1);
semilogx(phase(2:end-1,1), phase(1:end-2,2), '--r' )
But can it e improved even more? :P

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!