MATLAB $\tt butter$ function
You're right. That's why your anti-aliasing filter needs to be an analog filter. You need to restrict the bandwidth of the analog filter before sampling.

However, you can do part of the anti-aliasing filtering in the digital domain by first using a higher sampling rate and a simple analog filter, and down-sampling after applying a digital anti-aliasing filter. This is explained in the answers to this question. But in any case you'll need an analog filter first.