| MATLAB Web Server | ![]() |
Creating MATLAB Web Server M-Files
M-File Template
You use the M-file template to code your MATLAB application as you normally do. The template provides the additional code you need to accept input from your HTML input document and to return results to your HTML output document. An abbreviated version looks like:
function retstr = mfile_template(instruct, outfile)
% STEP 1
% Initialize the return string.
retstr = char('');
% STEP 2
% Set working directory.
% The variables INSTRUCT.MLDIR and INSTRUCT.MLID are
provided
% automatically to all MATLAB Web Server applications that
use
% the matweb program.
cd(instruct.mldir);
% STEP 3
% Get the HTML form input variables
my_input_variable_1 = instruct.my_input_variable_1;
% STEP 4
% Perform your MATLAB computations, graphics file creations,
% etc. here:
% STEP 5
% Put variables that you want to put into your HTML output
document in an output structure. You create an HTML output
document from OUTPUT_TEMPLATE.HTML.
outstruct.my_output_variable_1 = More MATLAB computations
creating ...
scalars, matrices, cell arrays, graphics files, etc.;
% STEP 6
% Call the function HTMLREP with the output structure you
just
% created and the filename you created from
OUTPUT_TEMPLATE.HTML.
% Replace <OUTPUT_TEMPLATE.HTML> with the name of the HTML
output
% file you created using OUTPUT_TEMPLATE.HTML.
% This call fills the string RETSTR to return and optionally
% writes the output as a file if a valid filename is given
as the
% second argument to the present function.
templatefile = which('<OUTPUT_TEMPLATE.HTML>');
if (nargin == 1)
retstr = htmlrep(outstruct, templatefile);
elseif (nargin == 2)
retstr = htmlrep(outstruct, templatefile, outfile);
end
webmagic M-File
The data entered on the webmagic1.html input document is automatically passed to MATLAB, which then runs the webmagic function. Notations in boldface refer to steps in the M-file template.
% Initialize the return string. (Step 1)
retstr = char('');
(Step 2. Not needed. No generated graphics.)
% Get the msize (string) variable. Convert to a number. (Step
3)
% Check the range.
if(~length(instruct.msize))
msize = 3; % Default empty field.
else
msize = str2double(instruct.msize);
if (msize > 20), msize = 20; end % Max size.
if (msize < 3), msize = 3; end % Min square.
end
% Save size as a char string in structure OUTSTRUCT. (Step
4, 5)
outstruct.msize = msize;
% Create magic square in output structure OUTSTRUCT.
outstruct.msquare = magic(msize);
% Get column, row, and diagonal sum. Put in OUTSTRUCT.
d = sum(outstruct.msquare,1);
outstruct.msum = d(1,1);
% Output the results and optionally write as a file if the
% filename was given as the second argument to WEBMAGIC.
(Step 6)
templatefile = which('webmagic2.html');
if (nargin == 1)
retstr = htmlrep(outstruct, templatefile);
elseif (nargin == 2)
retstr = htmlrep(outstruct, templatefile, outfile);
end
| Creating Input Documents | Creating Output Documents | ![]() |