MATLAB and Python are both well known for its usefulness in the scientific computing world. There are several advantages of using one over another. Python is preferable for most of the programmers because it’s free, beautiful, and powerful. MATLAB, on the other hand, has advantages like there is the availability of a solid amount of functions, and Simulink. Both the languages have a large scientific community and are easier to learn for the beginners. Though MATLAB, because it includes all the packages we need is easier to start with than the Python where we need to install extra packages and also require an IDE. The **proprietary** nature of the algorithms in MATLAB i.e., we cannot see the codes of most of the algorithms and we have to trust them to implement for our usage makes us sometimes hard to prefer MATLAB over the other options available like Python. Besides, these *proprietary* algorithms of MATLAB come at a cost!

In short, in order to excel in all of our scientific tasks, we need to learn to utilize both of them interchangeably. This is where the Python’s module **SciPy** comes in handy. MATLAB reads its proprietary “mat” data format quite efficiently and fastly. The SciPy’s module, “loadmat” and “savemat” can easily read and write the data stored in the Python variable into the “mat” file, respectively. Here, we show an example to use the data from MATLAB in the “mat” format to plot in Python on a geographical map, which Python can execute much efficiently than MATLAB.

## Saving the MATLAB variables into a “mat” file

In this example MATLAB script, we show how can we read the “mat” file in the MATLAB script and save it.

```
clear; close all; clc
%% Load the two matfiles with some data into the workspace
load python_export_CME_ATML_orig_vars;
load station_info;
%% Remove the unrequired variables from the MATLAB's memory
clearvars slat slon;
%% Saving the data from the "station_info.mat" file as a cell data type
stns={slons' slats' stn_name'};
%% Conduct some operations with the data and save it in "admF" variable
admF=[]; %intializing the matrix
std_slU=std(slU);
for i=1:length(slons)
ccU=corrcoef(dU(:,i),slU); %Making use of the available MATLAB functions
std_dU=std(dU(:,i));
admF=[admF ccU(1,2)*(std_dU/std_slU)];
end
%% Saving the output "admF" matrix into the "MATLAB_export_admittance_output.mat" file at a desired location.
save('../EOF_python/MATLAB_export_admittance_output.mat','admF')
```

## Using the data from the “mat” file in Python to plot on a geographical map

```
import scipy.io as sio #importing the scipy io module for reading the mat file
import numpy as np #importing numpy module for efficiently executing numerical operations
import matplotlib.pyplot as plt #importing the pyplot from the matplotlib library
from mpl_toolkits.basemap import Basemap #importing the basemap to plot the data onto geographical map
from matplotlib import rcParams
rcParams['figure.figsize'] = (10.0, 6.0) #predefine the size of the figure window
rcParams.update({'font.size': 14}) # setting the default fontsize for the figure
from matplotlib import style
style.use('ggplot') # I use the 'ggplot' style for plotting. This is optional and can be used only if desired.
# Change the color of the axis ticks
def setcolor(x, color):
for m in x:
for t in x[m][1]:
t.set_color(color)
# Read the two mat files and saving the MATLAB variables as the Python variables
ADF = sio.loadmat('MATLAB_export_admittance_output.mat')
admF=np.array(ADF['admF'])[0]
STN = sio.loadmat('station_info.mat')
slon=np.array(STN['slons'])[0]
slat=np.array(STN['slats'])[0]
## Converting MATLAB cell type to numpy array data type
stnname=np.array(STN['stn_name'])[0]
sname=[]
for ss in stnname:
sname.append(ss[0])
sname=np.array(sname)
## Plotting the admittance values
plt.figure()
offset=0.5
m = Basemap(llcrnrlon=min(slon)-offset,llcrnrlat=min(slat)-offset,urcrnrlon=max(slon)+offset,urcrnrlat=max(slat)+offset,
projection='merc',
resolution ='h',area_thresh=1000.)
xw,yw=m(slon,slat) #projecting the latitude and longitude data on the map projection
m.drawmapboundary(fill_color='#99ffff',zorder=0) #plot the map boundary
m.fillcontinents(color='w',zorder=1) #fill the continents region
m.drawcoastlines(linewidth=1,zorder=2) #draw the coastlines
# draw parallels
par=m.drawparallels(np.arange(21,26,1),labels=[1,0,0,0], linewidth=0.0)
setcolor(par,'k') #The color of the latitude tick marks has been set to black (default) but can be changed to any desired color
# draw meridians
m.drawmeridians(np.arange(120,123,1),labels=[0,0,0,1], linewidth=0.0)
cax=m.scatter(xw,yw,c=admF,zorder=3,s=300*admF,alpha=0.75,cmap='viridis') #plotting the data as a scatter points on the map
cbar = m.colorbar(cax) #plotting the colorbar
cbar.set_label(label='Estimated Admittance Factor',weight='bold',fontsize=16) #customizing the colorbar
plt.savefig('all_stations_admittance.png',dpi=200,bbox_inches='tight') #saving the best cropped output figure as a png file with resolution of 200 dpi.
```

You can upload the jupyter notebook to https://gist.github.com/ and then share it as guided from https://pythonandr.com/2016/07/18/sharing-ipython-jupyter-notebooks-via-wordpress/

LikeLike

Cool! I prefer using my beautiful editor for writing blogs, which I have customized for easy and fast use!😂 But using Jupyter Notebook is definitely an elegant approach, even for many more things like making slides.

LikeLike

If you cant afford Matlab you can use Octave,

I think the “trust” argument is rather weak, most often we see students using python packages that they find here and there, effectively putting their trust in other people’s coding capabilities. In Matlab you put your trust in professionals who do this since decades, there is an enormous code review and performance optimization work that goes into each line of code. Hence it seems natural to trust a Matlab implementation rather than a package somebody put online just as a favor.

LikeLike

MATLAB is definitely recommended at least for a beginner! There are many Python packages like Numpy, SciPy, Pandas etc are written by professionals too and is used widely even in industries. For students using Python packages from “here and there” can also be true for MATLAB. But these “here and there” packages are sometimes worth using! It’s never a bad idea to have more options available.

LikeLike