function 'subsindex' is not defined for values of class 'string'

This is roughly my code Only sometimes I have less or more inputs in the array "nam" In some situations i get the error " 'subsindex' is not defined for values of class 'string'" I looked for this, but could not find it and I do not understand what the error message means.
nam = ["t1" , "t3" , "t4"];
for j=1:3
figure
title( ['title start' nam(j) ] )
end

5 Comments

Must be somewhere else. There is no error if you just run the code above.
@Mattijs Mientki: please show is the complete error message. This means all of the red text.
I added the image of the error. Sorry, my code is quite long, so I can immagine it being somewhere else. But it could already help if you could explain me what the error message means, to help me where to look. Besides, that the error comes in sometimes with a colleague of mine and it is not always reproducable so this makes it difficult to show you the actual error.
Mattijs - is nam always an array or can it just be a string (in your code)? For example, if you have
someString = "hello";
someString(1)
What happens? Unfortunately, my version of MATLAB doesn't support creating strings (as opposed to character arrays) so I am wondering if, in some cases, your nam is just a string and not an array of strings and so treating the string object as if it were an array (using nam(j)) generates the error...
I think it can but also this code you have works fine for me. I think it has to do indeed with matlab versions. I just found out my colleague has a code which gives the error in matlab2018b and myself have matlab2017b and I have not errors with exactly the same code

Sign in to comment.

 Accepted Answer

Rename the variable named title in your workspace so you can use the title function again.
When the error message refers to subsindex, it means you're trying to use an object as the index into a variable but MATLAB doesn't know how to interpret that object as an index and the variable into which you're trying to index hasn't had its indexing implemented to handle that situation.
In your case, you've done something like this but with potentially many lines between the first and second line of my example (so you forgot you created a variable named title) and with title instead of f as the name.
>> f = (1:10).^2;
>> f("5")
Function 'subsindex' is not defined for values of class 'string'.

4 Comments

I just found it indeed that was the problem. Thank you for the explenation of the error message it helps to make it much more clear.
so the point is the subsindex error like above because of we had created a variable that same as function name, and then call that function which has the same name with our variable name ?
Correct. You intend to call a function, but because you have a variable with the same name as the function and variables are higher precedence than functions MATLAB treats that code as an attempt to index into the variable.
ow "variables are highers precedence than function" thanks a lot steven lord.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!