Running matlab is, or should be, entirely separate from managing swap space. You should separate the swap-creation/deletion parts into a separate script and then run that script with
sudo - either from the command line or from your main matlab-running script.
You should also quote your variables when you use them, not just when you define them. BTW, since your variables are fixed strings without any variable etc interpolation, you should use single-quotes when defining them. The same is true for your
dd command - only the
count= arguments need double-quoting. The others don't need quoting at all, but single-quotes are more appropriate if you choose to quote them.
Use double quotes when you want to embed other variables or command substitution in a variable or argument, single-quotes for fixed strings. BTW for dates, YMD sorts properly. DMY doesn't.
You shouldn't need to add swap space just because you're running matlab. Or any other program. If you need or might need more swap space then just add it to the system once, e.g. in
/etc/fstab and forget about it.
If you can resize your partitions so that the swap partition is slightly larger than you think you'll need then do so. If not, then add a swap file in
If swapping to an SSD, you should use the
discard option so that the blocks are trimmed on every reboot. e.g.
/dev/sda4 none swap defaults,discard 0 0
discard option definitely works on partitions. I'm not sure if it works on swap files too (I think it does but haven't seen any documentation that says so)
Even better than adding swap space is to install more RAM in the system if the hardware allows it. You mentioned in another post that you're using a Macbook Air, which has soldered-on RAM so doesn't allow RAM upgrades.
For other machines, this is the best option. DDR3-based systems with 4 DIMM sockets can easily and fairly cheaply be upgraded to 32GB (4x8GB DIMMs at around $50 each). DDR4-based systems with 4 DIMM sockets can easily be upgraded to 64GB (4x16GB DIMMs at around $100 each).
(prices are approximate, in AUD, for products available in .au, and current at time of writing)
It's possible, but harder to find and more expensive, to install 16GB DDR3 DIMMs....the price difference is large enough that it's probably worthwhile to just upgrade the motherboard and CPU to get DDR4.
zram can use some of your RAM as a compressed RAM block device. For example, for a zram swap device that uses up to 4GB of RAM:
modprobe zram num_devices=1
echo $((4 * 1024 * 1024 * 1024)) > /sys/block/zram0/disksize
swapon -p 100 /dev/zram0
You can use this if you also have a swap partition or swap file, but it makes more sense to use
zswap because there is no "intelligence" about what gets kept in ZRAM swap and what gets swapped to disk. When ZRAM swap is filled, all subsequent swapped data goes to disk until there's free space in the ZRAM again.
If you have a swap partition already, you can use
zswap to add a dynamically resizing RAM cache with compression to your existing swap space. You can tell it which compression algorithm to use, and the maximum percentage of RAM that it's allowed to use. For example:
echo 1 > /sys/module/zswap/parameters/enabled
echo lz4 > /sys/module/zswap/parameters/compressor
echo 50 > /sys/module/zswap/parameters/max_pool_percent
This will compress any data that is swapped out, and when it gets near the maximum RAM size, it starts using the swap partition on a Least-Recently-Used (LRU) basis, so the most recently used data is more likely to stay in RAM.
zswap have been in the mainline kernel for several years now.
zswap can be particularly useful if you also upgrade your RAM. e.g. I recently upgraded my main system to 32GB of DDR3 and also use an SSD swap partition with
zswap configured to allow up to 25% of RAM to be used for swap.