I’m fairly confident that the issue here is that you are setting the selected rows as a string of comma-separated indices instead of a list.
Try putting $stimnum_sample in your loop. If you’ve already tried this and get the same error then perhaps there’s an issue with your create of stimnum_sample. Try printing it to the console.