Plotting line between points

1 次查看(过去 30 天)
Adaptine
Adaptine 2016-11-18
编辑: Adaptine 2016-11-18
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

回答(2 个)

Adam
Adam 2016-11-18
编辑:Adam 2016-11-18
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 2016-11-18
编辑:Adaptine 2016-11-18
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

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by