There are lots of ways to do it depending on what method you want to use and how accurate you'd like it. If you want to do it by thresholding and erosion like you tried, try this:
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
grayImage=imread('C1 brain.jpg');
subplot(2, 2, 1)
imshow(grayImage);
axis('on', 'image');
impixelinfo;
title('Original Image', 'FontSize', fontSize);
lowThreshold = 20;
highThreshold = 255;
% Interactive threshold using Image Analyst's utility.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
% [lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, grayImage)
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Don't let top of skull touch top edge of image.
mask(1, :) = false;
subplot(2, 2, 2)
imshow(mask);
title('Initial Mask Image', 'FontSize', fontSize);
% Erode the mask some
radius = 29;
se = strel('disk', radius, 0);
mask = imfill(mask, 'holes');
mask = imerode(mask, se);
% Don't erase face and neck.
mask(400:end, 1:600) = true;
subplot(2, 2, 3)
imshow(mask);
title('Final Mask Image', 'FontSize', fontSize);
% Erase gray image
grayImage(~mask) = 0;
subplot(2, 2, 4)
imshow(grayImage);
title('Final Gray Scale Image', 'FontSize', fontSize);
g = gcf;
g.WindowState = 'maximized'
I've also attached my standard skull stripping demo.