MATLAB $\tt butter$ function I suspect I have a very basic misconception so I'm trying to clarify that here:

A signal should be filtered before it is sampled, correct? That is, I always sample an analogue signal at some sampling frequency after it has already been filtered.

However, MATLAB's butter function acts on sampled data. See the example code here:

[b,a] = butter(6,0.6);
dataIn = randn(1000,1);
dataOut = filter(b,a,dataIn);


But once you've sampled, the aliases are mixed up with the original signal so isn't this a very problematic way of doing things?
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.