The way to do it is to bin the target into bins depending on what makes sense for your target.
target = randn(100, 1);
binned_target = discretize(target, 5);
cv_new = cvpartition(binned_target, 'Holdout', 0.2, 'Stratify', true);
Warning: One or more of the unique class values in GROUP is not present in the training set. For classification problems, either remove this class from the data or use N instead of GROUP to obtain nonstratified partitions. For regression problems with continuous response, use N.
new_train_idx = training(cv_new);
new_test_idx = test(cv_new);
figure;
hold on;
H2_TRAIN = histogram(target(new_train_idx), 5);
H2_TEST = histogram(target(new_test_idx)), 5;
H2_TEST =
Histogram with properties:
Data: [20×1 double]
Values: [1 2 9 5 3]
NumBins: 5
BinEdges: [-3 -2 -1 0 1 2]
BinWidth: 1
BinLimits: [-3 2]
Normalization: 'count'
FaceColor: 'auto'
EdgeColor: [0 0 0]
Show all properties
legend([H2_TRAIN; H2_TEST], {'Binned Train', 'Binned Test'});
hold off;