MATLAB code in a sheet
A worksheet can hold MATLAB-language code. It runs in the page, on an independent implementation of the language (it is not MATLAB, and it is not made by or connected to The MathWorks, Inc.), prints and draws into the sheet, and hands chosen values to the Calcpad lines that follow, with their units. Nothing is sent to a server.
The point is that a MATLAB file you already have keeps working in a browser tab, next to the calculations that use its results,
and can leave again as ordinary .m files.
A block in a worksheet
Put the code between #matlab and #end matlab in a worksheet (.cpd):
"Beam check
#matlab
L = 6; w = 12; % m, kN/m
M = w*L^2/8
x = linspace(0, L, 61);
plot(x, w*x.*(L-x)/2);
xlabel('x (m)'); ylabel('M (kN m)'); grid on
% export: M
#end matlab
M_kNm = M*1kN*m
What the block prints appears in the sheet, in order. A figure appears where it was drawn. The names on the % export: line
become Calcpad variables below the block, and a plain number can then take a unit, as M*1kN*m does above. Exported
values can be numbers, vectors (a row or a column) and matrices. A block that fails shows the MATLAB error and its line
number, and the lines after it still calculate.
Several blocks in one worksheet run in order. Variables are not shared between blocks, but files are (see below).
Open a .m file
File > Open (or drag the file onto the page) opens a script as a worksheet with its code in a #matlab block. Names to
hand to the Calcpad lines go on the % export: line the page adds under the code.
A file that starts with function is a function file. It goes into the library instead of opening, and any block that calls
that function uses it, including the functions it calls in turn. A call to a function that is not there says which file to add.
Library .m files open in the editor untouched, with no Calcpad operator shortcuts.
Data files
A library file whose name the code writes as a string is visible to the block under that name:
#matlab
A = readmatrix('loads.csv');
total = sum(A(:))
writematrix(A*2, 'doubled.csv');
#end matlab
Add loads.csv to the library with File > Open or by dropping it on the page. The block can read and write text, CSV
and MAT files (readmatrix, readtable, csvread, dlmread, fopen, fgetl, fscanf, fileread, fread, fwrite,
save, load, writematrix, writetable and friends), make folders and delete files. A file one block writes can be read
by a later block in the same worksheet. Files a script writes are listed under its output; they live for that run and are not
added to the library.
A file name that the code builds at run time (['run' num2str(k) '.csv']) cannot be seen in advance, so that file is not
attached.
Plots
Line plots, scatter plots, bars, stairs, areas, stems, error bars, histograms, legends, titles, labels, log axes and subplot grids draw as pictures that print and export with the report. Surfaces, 3-D lines, images, contours and quiver plots are interactive charts: rotate, zoom and hover. Polar plots, filled patches and regions are not drawn yet, and the figure says so.
Take it with you
- File > Save as MATLAB project (.zip) writes a folder that unpacks and runs in MATLAB or Octave:
<name>.mwith the block code (one%% Block nsection each), the function files it calls, the data files it names, areport.htmlof the worksheet as calculated, and a README. It is meant for coursework and handing code to a colleague. - File > Save with its files (.cpz) writes one file holding the worksheet and the library files it uses. Opening a
.cpzhere puts the worksheet and its files back. It is an ordinary zip, so any unzip tool can open it.
The Calcpad lines of a worksheet (units, formulas, tables) do not become MATLAB code. Only the code in the blocks does.
Limits
The time limit for a block is 20 seconds; a block that runs longer is stopped, and the page asks whether to allow 120 seconds. Loops run at the speed of an interpreter in the browser, noticeably slower than desktop MATLAB; vectorised code is much closer.
Not supported at present: classdef objects, integers of the 64-bit kind beyond 2^53, matfile, fminunc, del2, apps and
interactive figures, and toolboxes beyond the core language and a large part of the numerical, signal, statistics and
optimisation functions. A symbolic integral to infinity returns nothing. The engine is tested against a set of self-checking
programs (linear algebra, ODEs, FFT, strings and cells, structures, plots, file input and output, symbolic work), and results
agree with known values; that is not a promise of identical output to MATLAB in every case. Check anything you will rely on, and
check a submission in MATLAB or Octave before you hand it in.
MATLAB is a trademark of The MathWorks, Inc. It is named here only to say what language the code is written in.