NLed
NLed

Reputation: 1865

Replot the same data point multiple times before continuing in Matlab

If I have the following code :

for t=1:length(s)  % s is a struct with over 1000 entries
  if s(t).BOX==0
    a(t,:)=0;
    elseif s(t).BOX==1
    a(t,:)=100;
  end
  if s(t).BOX==2
    b(t,:)=150;
    elseif s(t).BOX==3
    b(t,:)=170;
  end
  .
  .
  .

end
plot(a)
plot(b)
plot(c)

What I want to accomplish :

for n=1:length(s)

Plot the data point of a(n) at t=0, t=1, t=2
then
Plot the data point of b(n) at t=3, t=4, t=5
.
.
.
etc

So basically, each data point will be plotted at 3 values of t before moving to the next point.

How can I achieve that ?

EDIT

Something like this :

enter image description here

Upvotes: 0

Views: 182

Answers (1)

wakjah
wakjah

Reputation: 4551

If I'm understanding you correctly, and assuming a is a vector, you could do something like

% Your for loop comes before this

nVarsToPlot = 4;
nRepeatsPerPoint = 3;
t = repmat(linspace(1, nRepeatsPerPoint * length(s), nRepeatsPerPoint * length(s))', 1, nVarsToPlot);
genMat = @(x)repmat(x(:)', nRepeatsPerPoint, 1);
aMat = genMat(a); bMat = genMat(b); cMat = genMat(c); dMat = genMat(d);
abcPlot = [aMat(:) bMat(:) cMat(:) dMat(:)];
plot(t, abcPlot);

I'm a bit unclear on exactly what values you want your t to contain, but you essentially need a vector 3 times the length of s. You can then generate the correct data matrix by copying your [Nx1] vectors (a, b, c, etc.) three times (after transposing them to row vectors) each and stacking the whole lot into matrix, then transforming it to a vector with (:) which should come out in the right order as long as the matrix is constructed correctly.

Upvotes: 1

Related Questions