For amusement, a Maple version of the general solution, using direct numeric calculations without creating the intermediate values.
H := proc (M)
  local min_numdig, contrib_from_min, leftover, dp1, base_number, dig, dig_offset;
  min_numdig := floor(((1/9)*ln(10)+LambertW((1/90)*ln(10)*(9*M-1)*10^(8/9)))/ln(10));
  dp1 := min_numdig+1;
  contrib_from_min := (1/90)*10^dp1*(9*min_numdig-1)+1/9;
  leftover := M-contrib_from_min;
  if leftover = 0
  then
    dig := "9"
  else
    base_number := 10^min_numdig-1+floor(leftover/dp1);
    dig_offset := `mod`(leftover, dp1);
    if dig_offset = 0
    then
      dig_offset := dp1
    else
      base_number := base_number+1
    end if;
    dig := sprintf("
  end if;
  dig
end proc;
Note: this fails on 0 due to both a Maple technicality and a logic bug. The mathematics of it is presented without proof.
No MATLAB version of this will be provided, as the question is obviously a homework question.