title: Python 2.7: Variable "is not defined"
body: <p>I'm using <a href="https://physionet.org/cgi-bin/atm/ATM" rel="nofollow">Physionet's data base</a> for some tasks related to ECG signal analysis. I wanted to read .MAT files, extract the MLII readings on the file (located throughout row 1), adjust the signal to mV using "gain" and "base" (located in the .INFO filed also supplied by Physionet) and finally print the signal values and its period.</p>
  
  <p>I wanted to write a script that could do all of those things to all the files in one folder. Before this, I wrote one in which I could do everythin mentioned above and it worked nicely. </p>
  
  <p>But the script that would manage all the .mat and .info files in my folder is giving me problems with the variables. I tried using the 'global' command in the very beginning of my succession of IFs, but it kept sending a similar error message. </p>
  
  <p>This is the code:</p>
  
  <pre><code>import os
  import scipy.io as sio
  import numpy as np
  import re
  import matplotlib.pyplot as plt
  
  for file in os.listdir('C:blablablablabla\Multiple .mat files'):
      if file.endswith(".mat"):
          file_name=os.path.splitext(file)
          ext_txt=".txt"
          ext_info=".info"
          if file.endswith(".info"):
              f=open(file_name[0]+ext_info,'r')
              k=f.read()
              f.close()
              j=re.findall('\d+', k)
              Fs=j[9]
              gain=j[13]
              base=j[14]
  
          RawData=sio.loadmat(file)
          signalVectors=RawData['val']
          [a,b]=signalVectors.shape
          signalVectors_2=np.true_divide((signalVectors-gain),base)
          ecgSignal=signalVectors_2[1,1:]
          T=np.true_divide(np.linspace(1,b,num=b-1),Fs)
          txt_data=np.array([ecgSignal, T])
          txt_data=txt_data.T
          f=open(file_name[0]+ext_name,'w')
          np.savetxt(file_name[0]+ext_txt,txt_data,fmt=['%.8f','%.8f'])
          f.close()
  </code></pre>
  
  <p>The error message I get is: </p>
  
  <pre><code>&gt; File "C:blablablablabla\Multiple .mat files\ecg_mat_multi.py", line 24, in &lt;module&gt;
      signalVectors_2=np.true_divide((signalVectors-gain),base)
  NameError: name 'gain' is not defined
  </code></pre>
  
  <p>The problem comes with the variables 'gain', 'base' and 'Fs'. I tried to define them as global variables, but that didn't make a difference. Can you help me fix this error, please?</p>
  
  <p>Thanks a lot for your time and help.</p>
  
  <p>EDIT 1: copied the error message below the script. 
  EDIT 2: Changed the post title and erased additional questions. </p>