Plotting 3 unequal vectors

Hi guys,
having a little trouble plotting this. I want to plot timeMat in the x axis and other vectors (GrayValScan,StoreData) in the y axis. Note: GrayValScan and StoreData are both different dimensions from each other as well.
timeMat= abs(-860:0);
i=3;
n=107;
while n<=107
figure
hold on
plot(timeMat,GrayValScan)
plot(timeMat,storeData(:,i))
hold off
i= i+1;
end
I tried doing this based on another answer and repeat it for the other vector. Nothing worked.
maxLength = max(timeMat);
GrayValScan(length(timeMat)+1:maxLength) = 0;
Any suggestion? Thanks in advance

 Accepted Answer

I would use the linspace function:
GVStimeMat = linspace(-860, 0, length(GrayValScan));
SDtimeMat = linspace(-860, 0, size(storeData,1));
i=3;
n=107;
while n<=107
figure
hold on
plot(GVStimeMat,GrayValScan)
plot(SDtimeMat,storeData(:,i))
hold off
i= i+1;
end
NOTE I don’t have your data, so this is UNTESTED CODE. It should work.

4 Comments

it works for the most part. It still does give me an error after it plots however
Index exceeds matrix dimensions.
Error in test (line 49)
plot(SDtimeMat,storeData(:,i))
I did have a question regarding this line
SDtimeMat = linspace(-860, 0, size(storeData,1));
what does the "1" signify?
With respect to the matrix dimensions problem, check to see what size ‘storeData’ is. Your column-counter ‘i’ cannot exceed the number of columns in ‘storeData’. (The ‘SDtimeMat’ vector has already been created, so it should not be the problem.)
Since by looking at your code, you’re plotting individual columns of ‘storeData’, I set ‘SDtimeMat’ to be equal to the row-length (number of rows) in ‘stordData’. In the size function, the first dimension is the row-length (the second column-length, the third the number of ‘pages’, etc.), so for a matrix, the ‘1’ tells size that I want it to return the number of rows in ‘storeData’ so ‘SDtimeMat’ will be the same length as the column you’re plotting.
Note: You’re while loop is testing ‘n’, but you’re not modifying ‘n’ within the loop. You seem to be limiting it to 107 in your test code, so the loop will only run once here, but that could be something to consider.
Thank you for the explanation. I didn't realize that the n value didn't do anything in the loop. I took it out and dealt with just "I".
As for the dimension mismatch error I was getting, it was conditional error. I used a <= instead of just <
My pleasure.
If you know in advance the limits ‘i’ should have, you can use a for loop. That might be more efficient, since it incorporates the incrementing of ‘i’ and would eliminate the separate counter increment assignment. (A while loop is best if you’re incrementing and testing a value calculated within a loop.)

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!