MATLAB Debugging Error Message

test_idx =
1
1
7
13
19
25
31
37
43
49
55
for k1 = 1:length(test_idx)-1
TestLbl(k1,:) = sprintf('Test #%d',k1);
end
Subscripted assignment dimension mismatch.
Error in test>LabelPeak_Callback (line 563)
TestLbl(k1,:) = sprintf('Test #%d',k1);
What does this mean?

 Accepted Answer

Walter Roberson
Walter Roberson on 4 Mar 2013
When you use sprintf() with a %d format, the number is converted into the minimum number of characters needed for it. For 1, 1, and 7, that is one character, so for those three the resulting strings are all the same size. But then you reach 13 and that takes two characters to output, so the resulting string is longer than the ones before. You are using TestLbl(k1,:) as the destination so you are trying to write a row which is longer than the existing rows. You cannot have rows of different lengths in a character array.
You need to assign to TestLbl{k} (a cell array entry) or else you need to ensure that the strings are all the same size such as using %3d instead of %d. %3d means to use at least 3 characters, so for example space-space-7 for 7.

31 Comments

Thank you!
Suppose I have another set:
a= 0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
test = [find(a == min(a)) < find(a == max(a))]+1;
Now it usually works for other data sets but because the sequence is as follows:
0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
it doesn't decline at the end, only steadily increases up to a maximum.
What 'test' does is detect a peak but it can't without a smaller value after 0.515 .
find( a == min(a)) gives:
1
7
13
19
25
31
find(a == max(a)) gives:
5
6
11
12
17
18
23
24
29
30
35
36
The error I receive is:
Error using <
Matrix dimensions must agree.
I think this has to do with the fact that I have two values, that is 0.515 and 0.515 that are maximum and the same. How can I avoid this error?
That sounds like a completely different Question.
The answer to it is going to depend on what you want done in this situation. While you are at it, you should also consider possibilities such as
5 10 2 3
where the global minimum is after the global maximum.
T
T on 4 Mar 2013
Edited: T on 4 Mar 2013
Well with the above, wherever it detects a local maximum, and local minimum, it finds those elements and if it satisfies the inequality <.
Does
test = [find(a == min(a)) < find(a == max(a))]+1;
not already satisfy
5 10 2 3?
I just want to be able to label these peaks, which I have done but came across this unique dataset and I'm not sure how to tank into account the repeated consecutive entries.
0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
min(a) and max(a) are non-local min and max. For a=[5 10 2 3], the find(min) would report 3, find(max) would report 2, then 3<2 is false, which is numeric 0, add 1 to get 1... and then?? Clearly I could have added more numeric padding before the 10, but 1 would still have been reported (the same as if there was no peak because the values were strictly descending)
If you are looking for local peaks, look at diff(a)<0
Meanwhile, consider find(a == min(a),1) and find(a == max(a),1)
For a=[5 10 2 3],
find(max(a)) and find(min(a)) gives 1
find(a == max(a)) and find(a == min(a))
Actually nevermind. I don't even need the inequality.
T
T on 4 Mar 2013
Edited: T on 4 Mar 2013
Actually I still encounter the same problem with repeated values:
idx = [1;find(a >= 0.9*max(a))];
for k = 1:length(idx)-1
TestLbl(k,:) = sprintf('Test #%3d',k);
end
for j = 1:length(idx)-1
text(Times(2,j), 0.75, TestLbl(j,:), 'FontSize',12, 'HorizontalAlignment','right')
end
Gives me the following error:
Index exceeds matrix dimensions.
The above works fine for most datasets that I am dealing with except the one mentioned in the original post:
0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
Which line is the error occurring on? What is size(Times) ?
Why are you still assuming that all the labels will come out as the same size?
for k = 1 : length(idx)-1
text(Times(2,j), 0.75, sprintf('Test #%d', k), 'FontSize' 12, 'HorizontalAlignment', 'right');
end
The error occurs here:
text(Times(2,j), 0.75, sprintf('Test #%d', k), 'FontSize' 12,
not size(Times) but text(Times(2,j).
Times =
1.0e+004 *
0.4494 0.6111 0.7298 0.9002 1.0187 1.1038
0.4505 0.6116 0.7300 0.9006 1.0189 1.1042
0.4512 0.6120 0.7303 0.9008 1.0191 1.1048
0.4525 0.6126 0.7307 0.9011 1.0194 1.1061
0.4536 0.6132 0.7311 0.9014 1.0196 1.1071
0.4559 0.6137 0.7315 0.9017 1.0199 1.1083
0.4571 0.6143 0.7319 0.9020 1.0202 1.1093
Same size? Are you referring to sprintf?
Yes, as discussed above, sprintf() will produce strings of different lengths for format %d according to how many characters are needed to represent the value. You were then storing the potentially-different-length strings into TestLbl(k,:) which is a construct that would only permit strings that were all the same length. The alternative code I gave avoids this problem.
Now when the error occurs, please show the value of "j" and show the value of size(Times) (not size(Times(2,j))
T
T on 4 Mar 2013
Edited: T on 4 Mar 2013
so for this data set:
a= 0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
0.3230
0.3570
0.3560
0.3750
0.5150
0.5150
Putting the break (--->)
idx = [1;find(a >= 0.9*max(a))];
for k = 1:length(idx)-1
------------> TestLbl(k,:) = sprintf('Test #%3d',k);
end
for j = 1:length(idx)-1
text(Times(2,j), 0.75, TestLbl(j,:), 'FontSize',12, 'HorizontalAlignment','right')
end
size(Times)
ans =
7 6
The error occurs at the text(Times(2,j),.....
Walter Roberson
Walter Roberson on 4 Mar 2013
Edited: Walter Roberson on 4 Mar 2013
And what is the value of "j" at that point? And what is length(idx) ? 13, right? How did you decide that Times should have 6 columns?
T
T on 5 Mar 2013
Edited: T on 5 Mar 2013
The value of j at that point is 7. length is 13. Times should have 6 columns, as determined by something else. Nevertheless the number of columns for Times is correct.
If the width of Times is not being determined by the same process that determines the peak positions, then one or the other is incorrect.
T
T on 5 Mar 2013
Edited: T on 5 Mar 2013
It works for two other data sets and I suspect that the reason why it doesn't work for this one is because there are two maximum and two minimum for which test_idx gets those elements for thereby increasing the the length of that vector when it fact it should be half of what it is.
So, how does one, when using test_idx = [1;find(a >= 0.9*max(a))] take into account for repeated maximum?
When does not have to check for global max and min and I have set, I removed the inequality because it's not necessary. I don't even know why I bothered. All I needed was the maximum.
T
T on 5 Mar 2013
Edited: T on 5 Mar 2013
Actually I can just use mode? Nope...
find(diff(wt)==0) works but only for that particular case and fails for the rest. The or operator will not work with test_idx because the inputs do not have the same size. I could try removing the repeated index then take the maximum.
T
T on 5 Mar 2013
Edited: T on 5 Mar 2013
I got something that works:
if find(diff(a)==0)
what = (diff(a)==0)
label = find(what == 1)
else
label = [find(a >= 0.9*max(a))]
end
test_idx = [1;label]
however,
for k1 = 1:length(test_idx)-1
TestLbl(k1,:) = sprintf('Test #%3d',k1);
end
gives me odd results for the data set mentioned for the original post.
Even though I don't get an error, the output for the data in the original post doesn't display the word Test #.
It probably has to do with the order of the if statements.
Suppose you had duplicates that were not at the maximum ?
Do I need to be concerned with duplicates not at the max?
find(a >= 0.9*max(a)) only looked for max values, which is all I need and what was being repeated was the maximum values.
find(diff(a)==0) finds duplicates even if they are not at the maximum. And if it does then you do not proceed to testing for 0.9 times the maximum.
T
T on 7 Mar 2013
Edited: T on 7 Mar 2013
You're right.
I was playing with histc(a)
So if
a = [1 2 3 4 5 5 6 8 9 1 2 6 9 9 12];
I = histc(a,a(1):a(end));
find(I>1)
ans =
1 2 5 6 9
However when I try it with the data above, vector a is not expressed as
a = [ ... ]
How do I convert our 'a' into this format [ ] as opposed to just
a = 2 4 5 6 7 8 and so on?
Or I could try max(a) == mode (a)
T
T on 7 Mar 2013
Edited: T on 7 Mar 2013
When I use if (max(a) == mode(a))==1 and place a break on this line, it prompts mode.m and I'm not sure why.
T
T on 7 Mar 2013
Edited: T on 7 Mar 2013
if (max(a) == mode(a))==1
what = find(diff(a)==0)
test_idx = [1; what]
else
label = [find(a >= 0.9*max(a))]
test_idx = [1;label]
end
should work but it doesn't. Do you know what's wrong ?
Does it give an error message?
No it just doesn't show the label for the dataset in the original post, as with every other attempt I've made.
Actually I was wrong,
when ever I put the break on
if (max(a) == mode(a))==1
A new window pops up mode.m with the green arrow pointing to
if nargin<2
Do you know what's wrong?
Recode as
Tmode = mode(a);
Tmax = max(a);
if Tmode == Tmax
That way you can examine the parts of the calculation.
mode() really seems unlikely to me to be useful in this situation. Did you notice that if there are multiple values with equal maximum counts, that it returns the smallest of them? So
[1 2 3 3 4 5 6 7 7]
would return 3
that works thanks.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!