strtok doesn't work on my matlab version
4 views (last 30 days)
Show older comments
Pestiaux Marianne
on 12 May 2017
Edited: Walter Roberson
on 12 May 2017
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
0 Comments
Accepted Answer
Walter Roberson
on 12 May 2017
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)
John D'Errico
on 12 May 2017
Edited: John D'Errico
on 12 May 2017
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
Walter Roberson
on 12 May 2017
Edited: Walter Roberson
on 12 May 2017
Yes, I have seen uiimport return a string object in at least one case.
Steven Lord
on 12 May 2017
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.
0 Comments
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!