Ever played with LTN? That would help a little, because its actually quite common to have mixed warehouses and thinking of it, using train ID is somewhat simillar to reading train schedules with LTN stop´s output combinator.
Actually a brilliant idea
Wall of text incoming, but i like to take the problem apart into smaller steps
If you like a TLDR, skip to the last line and have it build for you
What went wrong?
In your case, inserters kept some items in their hands because the last iron train was full. That iron gets dropped into the stone train, even if they are disabled
As already said, filtered wagon slots is the easiest way in your case.
But the loading time could be improved alot, with the side effect of not spilling items, at the tiny cost of not fully loaded wagons.
Prevent inserters from keeping items in their hands
A (vanilla) wagon has 40 slots, with something like stone (stack size 50) thats 2000 stone.
You have to read train contents and stop loading when it reaches 1950 (depending on inserter stack size, but one is usually enough with inserters), times the number of wagons.
That way, you will have like 1970 items loaded, because inserters continue to unload into the last slot.
That one slot will not get loaded fully.
The next train will not get those leftovers that you have now.
Filtered loading of chests providing multiple items
Usually i use filter stack inserters, with Bobs, they are fast enough.
But I have tried it with miniloaders. Since they can hold more than one inserter and you actually need two of them in line, you need to stop at least 2 stacks before a full load is reached. Thats because you have 6 loaders per wagon * 2 tiles with loaders. I believe miniloaders have like 4 or 6 invisble inserters inside. You could have like 50 items or more in transit between your chest and the wagon.
However, with loaders it needs some testing whats left in the loaders after you stop letting them.
Gets worse with barrels. Dont try that.
That was only the theory
Lets move on to implementing that
What you do is
Set filters dynamicly according to train ID, making loading alot faster than your current setup
Which, as I understand it, has one filtered loader per wagon and item type active, depending on the train ID, the rest is disabled.
Example
IF T = 917 (example for train ID);Output "item"
The amount is (item stack size * wagons * (wagon slot number -1)) which you set manually in a constant combinator before your deciders. You can combine all items on all inputs, since you only let one through anyway.
Connect those outputs of deciders to the output of an arithemtic that inverts (anything * -1) anything coming from the train stop, which needs to be set to "read train contents" as well as your "read train id"
Setup:
Chest->filter loader->normal loader->wagon
Filter loader is set to "set filter" (not enable), all filter loaders are connected together to the deciders
-Train sends ID, filter is set, filtered miniloader loads into a normal miniloader, that empties into the wagon
-Train is "full", filter signal is "removed".
*)
-Normal miniloader still loads what it last got from the filter loader
-Wagon gets what was left from previous 2 steps
You need to set leave condition to "inactive", not "full" or "item count"
hope that helps
* why no filter is set any longer, a FYI
Negative signals will not work as "set filter" condition
If the combined signal gets negative, because the wagon has more than we want (1950 demand - 1970 wagon load = -20 in the above example), the filter is "removed" and the loading is stopped
Also "set filter" controlled inserters that dont get a signal thats a movable item will stop working (you could have any other signal like [A] or [1] present. Which is handy, since you will have the train ID on that cable too.
That one actually doesn´t matter, since its negative because of the inverter
If you´d post a BP, i could change it according to what i suggested, but i think i´ll need a link to the chest enlarger mod that you seem to use, not sure there´s only one.