strtok doesn't work on my matlab version
Show older comments
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
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
Pestiaux Marianne
on 12 May 2017
Please run this command immediately before the line where the error occurs:
size(j)
and tell us exactly what it displays.
Pestiaux Marianne
on 12 May 2017
Pestiaux Marianne
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.
John D'Errico
on 12 May 2017
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.
Walter Roberson
on 12 May 2017
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});
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
0 votes
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
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!