strtok doesn't work on my matlab version

Hello,
I'm a begium student. My function consist of select the month of September. That's why i ask to delete the lignes when the 7 characters of my vector date is different of 9 (because the date format is 2016-09-..) It works on my university computer but i have another version of matlab on mine (i get it with matwork), can you explain why the "strtok" doesn't work here.
Thank you very much

 Accepted Answer

Your elements are string arrays, not character vectors. To access the 7th character of j, you need to use j{1}(7)
There are some routines that used to return cell arrays of character vectors by default but now return string arrays instead by default. If you are not expecting it then that can lead to problems.

More Answers (2)

No. It is not that strtok does not work.
If your code has an error in it, then of course it will fail. But it was your code that failed here, not strtok.
The offending line was:
token = strtok(j(7));
where we are told that index exceeds matrix dimensions. If j is a vector with length less than 7, what should MATLAB do?
It returns an error, because you tried to access the 7th element of j, when j has less than 7 elements.
Of course, if you knew how to use the debugger, or even easier, to read the error message, you would have learned this before ever having to ask the question. Some of the most important skills you can learn are how to use the debugger and how to read error messages. Such debugging skills will be crucial when programming in virtually any language.

10 Comments

yes i agree but i don't understand why because i told him to take the first colomn of this matrice and as you can see i do not exceed matric dimensions... I thought that maybe it was about my other matlab version as it work on my university computer don't you think ?
Please run this command immediately before the line where the error occurs:
size(j)
and tell us exactly what it displays.
like that ? (here is the all script and the matrice date, maybe you will better see)
Stephen23
Stephen23 on 12 May 2017
Edited: Stephen23 on 12 May 2017
Please put the size command on the line immediately before the line where the error occurs, and certainly not on the line after.
Stephen23
Stephen23 on 12 May 2017
Edited: Stephen23 on 12 May 2017
@Pestiaux Marianne: The variable j is scalar. It has size 1x1.
Clearly it is impossible to get the seventh element out of an array that only has one element (the scalar array j has only one element). Where is the seventh element in a one element array? (hint: nowhere, because it does not exist, thus the error).
Exactly like John D'Errico wrote: that code is buggy, and you need to figure out how to fix it: you would be much faster and more successful using the debugging tools, or even starting by going line-by-line and confirming that they do what they are intended to do: read the documentation for each operation and function, and checking the input and outputs of every line.
So j is a SCALAR, i.e., a 1x1 array.
What is j(7)?
j(7) does not exist, ergo the error message. Why it is that, you need to learn to use debugging tools to know. Look at how you created j, or the file it came from.
Notice the double quotes around the strings. You have items that are the new string datatype.
Did TMW change uiimport to return strings ?
@Pestiaux Marianne: you could try changing line 10:
j = char(date{i});
Yes, I have seen uiimport return a string object in at least one case.

Sign in to comment.

Set a breakpoint on line 5 of your code (or set an error breakpoint) then run your code. When you reach the breakpoint, look at the value of the variable j. It's probably not going to be what you think it should be. Then you should work your way backwards to find the root cause of the difference between what it should be and what it actually is.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!