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
1 2 3 4 5 6 7 8 9 10 11 |
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:
1 2 3 4 5 6 7 |
% 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:
1 2 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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:
1 |
cat url.txt | xargs -n2 bash -I 'wget {} -O {}' |
where the url.txt
is like
1 2 3 4 5 6 |
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=0
in a txt file and run:
1 |
cat files.txt | xargs -n1 -P2 wget -q |
where -q
is quiet wget, otherwise wget output will jam the window, -n1
is output one by one and -P2
is parallel with 2 threads
Use matlab in Terminal
The command is
1 |
matlab -nodesktop |
Only run script and record all output as log
1 2 |
matlab -nodesktop -r 'scripts.m' -logfile 'log.txt' |
Note: matlab for loop has poor compatibility with global variables. Maybe while loop is better.