In a MATLAB function I am writing, I am generating a figure. The figure is displayed when the function is executed. I need to save the figure as a JPEG image. To do that, I
The Matlab figure export dialog and the saveas() function lack a lot of desirable functionality. Especially, savas() cannot create a custom resoultion image which is why your results look poor. For creation of bitmap images I highly recommend using the third-party function export_fig. By adding the following code to your function (including the maximizing trick)
set(gcf, 'Color', 'white'); % white bckgr
export_fig( gcf, ... % figure handle
'Export_fig_demo',... % name of output file without extension
'-painters', ... % renderer
'-jpg', ... % file format
'-r72' ); % resolution in dpi
I created this image: (use "show image" or similar in your browser to get the original size)

For higher quality use higher resolutions of 150 or even 300 dpi (for print). Instead of maximizing the figure window, for most applications it's suitable to define the axis size to obtain an image of the desired size:
unitSave = get(figureHandle, 'Unit'); % store original unit
set(figureHandle, 'Unit', 'centimeters'); % set unit to cm
set(figureHandle,'position',[0 0 width height]); % set size
set(figureHandle, 'Unit', unitSave); % restore original unit