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

 채택된 답변

Star Strider
Star Strider 2016년 8월 22일

1 개 추천

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

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?
Star Strider
Star Strider 2016년 8월 22일
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.
jchris14
jchris14 2016년 8월 22일
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 <
Star Strider
Star Strider 2016년 8월 22일
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.)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2016년 8월 22일

댓글:

2016년 8월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by