**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# MATLAB Debugging Error Message

1 view (last 30 days)

Show older comments

### Accepted Answer

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

T
on 4 Mar 2013

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?

Walter Roberson
on 4 Mar 2013

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
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

Walter Roberson
on 4 Mar 2013

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)

Walter Roberson
on 4 Mar 2013

find(a == max(a)) and find(a == min(a))

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

Walter Roberson
on 4 Mar 2013

Edited: Walter Roberson
on 4 Mar 2013

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

T
on 4 Mar 2013

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?

Walter Roberson
on 4 Mar 2013

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
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
on 4 Mar 2013

Edited: Walter Roberson
on 4 Mar 2013

Walter Roberson
on 5 Mar 2013

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
on 5 Mar 2013

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.

Walter Roberson
on 5 Mar 2013

Suppose you had duplicates that were not at the maximum ?

Walter Roberson
on 7 Mar 2013

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)

Walter Roberson
on 7 Mar 2013

Does it give an error message?

Walter Roberson
on 9 Mar 2013

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

### More Answers (0)

### See Also

### Categories

### Tags

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

Americas

- América Latina (Español)
- Canada (English)
- United States (English)

Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)