In my limited experience with Matlab, I find concatenating data to form a complex waveform easier then trying to generate all of it in one step. Hope this helps a little.
For rectangle again concatenating step functions may work. There was also the 'rectpuls' function that was generating some kind of a rectangle signal but not sure how it works.
Code:
r= 0;
for i=1: 1: 10
r= [r s];
end
Again those are what I came up with my limited experience, there must be much better ways to achieve those.