Matlab probably isn't particularly well suited for this, but may work. It's not really like C other than that it has many of the usual program flow statements (if, else, while, for etc.), but so do almost all languages.
In your code, one thing you should definitely change is to take out the creation of the recObj. There shouldn't be a need to recreate the object each time. It might still work though.
One way to periodically do something with the recorded audio data (e.g. display it) would be to use the built-in timer callback-function property. That way you could start the recording in a non-blocking way, i.e. keep doing other things while it's "recording". However, since you are new to Matlab, you probably don't know what that means. In a way the function is a bit like an interrupt service routine, and the callback-function property is similar to a function pointer.
Create a function and make sure it's in your Matlab path:
function audiorecPlot(arObj, ev)
sr = get(arObj,'SampleRate');
tp = get(arObj,'TimerPeriod');
nSamples = get(arObj,'TotalSamples');
allAudio = getaudiodata(arObj);
startIndex = max(0,floor(nSamples-sr*tp));
plot(allAudio(startIndex:nSamples));
end
Then you can do the rest from the Matlab command line if you want:
recObj=audiorecorder; %creates the recorder object
set(recObj,'TimerFcn',@audiorecPlot); %sets the timer callback function
set(recObj,'TimerPeriod',0.1) %sets the timer period to 0.1 sec
record(recObj); %start the recording - the plot should appear and update every 100ms
stop(recObj); %stop when you want to
I haven't tested this extensively, so it probably needs to be optimised a bit. E.g. you probably want to set the y-axis scaling to a fixed range.