Matlab Skills
Partialy load and save .mat file
Matlab is RAM-greedy.
Can load/write .mat files partially to mitigate it.
{Matlab-doc}
Quick start
exampleObject = matfile('topography.mat')
varlist = who(exampleObject)
[nrows,ncols] = size(exampleObject,'B');
load('example.mat','B');
save('mycopy_durer.mat','-v7.3')
save('example.mat','-append','B');
To load and partially with regular expression:
% laod all vars except for waveforms
load GR_LQ_22may2019 -regexp ^(?!waveforms$).;
% run and save new locsArray
run('./eventRepoMT.m');
save('locsArray.mat', 'locsArray','-v7.3');
disp(size(locsArray))
Note that MatLab has 2 ways to call functions:
load durer.mat X % command form
load('durer.mat','X') % function form
Renew saved figure files
retrieve data from matlab figure file for new plot.
For python user, matlab plot functions are almost the same as matplotlib, except no prefix like plt.* needed.
Ref: https://www.mathworks.com/matlabcentral/answers/100687-how-do-i-extract-data-from-matlab-figures
The interactive notebook (.mlx file) is extremely handy for development. It can convert the block into local function, increase the recallability of the code blocks. Here shows a local function for retrieve the data in figure file and replot them in new layout.
newFig = replot('./gr_lq_justP.fig', 'Pressure (MPa)');
newFig = replot('./load_AE.fig', 'Load (lb)');
function newFig=replot(oldFigPath, ylabels)
fig = openfig(oldFigPath);
dataObjs = findobj(fig, '-property', 'XData');
xData = dataObjs.XData; yData = dataObjs.YData;
newFig = figure()
fontsize(newFig, 12, "points");
set(newFig, 'Units', 'inches', 'Position', [0, 0, 2.5, 2]);
ln = plot(xData, yData, 'k'); xlabel('Time (s)'); ylabel(ylabels)
end
Basic
data structure
{,}
for cell array, analogous to python list[
,]
for array(
,)
for indices
Variables general info
Use
whos, can combine it with
-regexp. {Link}
batch run matlab
Introduce a unix command: xargs
It will convert the stdin to stdout.
See detailed Chinese introduction in Here
So option:
-p
get permission for each run-n2
output 2 each batch-I {}will replacewith the output-P 2
for parallel with 2 threads, but better use with-n1
to pass the task one by one
batch downloading:
cat url.txt | xargs -n2 bash -I 'wget {} -O {}'
where the
url.txtis like
https://www.xxx.yyy/a1 filename1 https://www.xxx.yyy/a2 filename2 https://www.xxx.yyy/a3 filename3
But now for simplicity, I just input the dropbox link without
?dl=0in a txt file and run:
cat files.txt | xargs -n1 -P2 wget -q
where
-qis quiet wget, otherwise wget output will jam the window,
-n1is output one by one and
-P2is parallel with 2 threads
Use matlab in Terminal
The command is
matlab -nodesktop
Only run script and record all output as log
matlab -nodesktop -r 'scripts.m' -logfile 'log.txt'
Note: matlab for loop has poor compatibility with global variables. Maybe while loop is better.