Saturday, October 11, 2025
HomeLanguagesSciPy – Input and Output

SciPy – Input and Output

In this article, we learn about SciPy input and output. In order to handle inputs and outputs of multiple formats, Scipy.io package provides multiple methods.

Some of the formats that can be handled by the Scipy.io package are:

  • Matlab
  • Netcdf
  • IDL
  • Arff
  • Matrix Market
  • Wave

Among this Matlab is the format which is used very frequently. 

Now let’s see the functions used to load and save a .mat file.

  • Firstly we need to use loadmat() function to load the Matlab file.
  • Secondly, we will use savemat() function to save the Matlab file.
  • Finally, whosmat() method is used to list the variables inside a Matlab file.

Below are various examples based on the above explanations which depict how to take input and display output using scipy module.

Example 1: Scipy program to take an integer input and display it.

Python3




import scipy.io as syio
  
# Save the mat file
n = 1706256
syio.savemat('num.mat', {'num': n})
  
# Load the mat File
matlab_file_contents = syio.loadmat('num.mat')
print(matlab_file_contents)
  
# printing the contents of mat file.
matlab_file_contents = syio.whosmat('num.mat')
print(matlab_file_contents)


Output:

Example 2: Scipy program to take an array input and display it.

Python3




import scipy.io as syio
  
# Save the mat file
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
syio.savemat('arr.mat', {'arr': arr})
  
# Load the mat File
matlab_file_contents = syio.loadmat('arr.mat')
print(matlab_file_contents)
  
# printing the contents of mat file.
matlab_file_contents = syio.whosmat('arr.mat')
print(matlab_file_contents)


Output:

Example 3: Scipy program to take string input and display it.

Python3




import scipy.io as syio
  
# Save the mat file
string = 'Geeks forneveropen!'
syio.savemat('str.mat', {'str': string})
  
# Load the mat File
matlab_file_contents = syio.loadmat('str.mat')
print(matlab_file_contents)
  
# printing the contents of mat file.
matlab_file_contents = syio.whosmat('str.mat')
print(matlab_file_contents)


Output:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11882 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6839 POSTS0 COMMENTS
Ted Musemwa
7102 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS