How to use Matlab to plot and save data in the Python Programming Language

Source: Internet
Author: User

During the application of the Python programming language, if you want to use the Python programming language to call Matlab to plot and save data, you can refer to the following articles to learn more about it, the following is the main content of the article. I hope you will have some gains.

Call Matlab to plot and save data

Recently, during my experiments, I needed to plot and save the obtained data in Matlab. A small problem is that there are not only data in the original data file, but also some comments before the data, each line starts ). It is troublesome to use Matlab to plot data directly. Because data cannot be processed directly, you can extract the data separately and save it as a file.

Or you need to use Matlab to write a program to filter text. However, Matlab is not its strength in text processing. I am not satisfied with either of these two methods. Python once again provides me with a solution. On the one hand, the Python programming language has strong text processing capabilities, and on the other hand, Python provides direct calls to interactive programs such as Matlab, therefore, it is undoubtedly appropriate to write a Python script to complete this task. The following is the implementation code:

     
     
  1. import os  
  2. import string  
  3. 1filepath = "d:\\\\exp\\\\chgeff_lar_1" 
  4. 2filename="chgeff_lar_1" 
  5. 3id = open(filepath, 'r')  
  6. 4lines = fid.readlines()  
  7. 5fid.close()  
  8. 6x = []; y = []  
  9. 7for line in lines:  
  10. 8if line[0]=="#" or len(line)==1:  
  11. 9continue  
  12. 10else:  
  13. 11xval, yval = string.split(line)  
  14. 12x.append(float(xval))  
  15. 13y.append(float(yval))  
  16. 14id = open(filename+'.m', 'w')  
  17. 15fid.write("""  
  18. 16x = %s  
  19. 17y = %s  
  20. 18plot(x, y)  
  21. 19xlabel('Particle diameter (nm)')  
  22. 20ylabel('Charging efficiency')  
  23. 21print -deps  %s.eps  
  24. 22pause(10)  
  25. 23"""  %  (x, y, filename))  
  26. 24fid.write("exit")  
  27. 25fid.close()  
  28. 26cmd = "d:\\\\matlab6p5\\\\bin\\\\win32\\\\matlab.exe -nodesktop -r  " + filename  
  29. 27os.system(cmd)  
The above 6-13 sentences implement two functions. One is to filter the text (8-9 sentences). By checking the first character of each line and the length of the Line, remove the comment line and empty line. The second is to automatically allocate each row of data read to two variables x and y (10-13 sentences ). Python then writes a set of Matlab code (16-24 sentences) to the file filename. m. Finally, the system function of the OS module is used to call Matlab plot and save (26-27 sentences ). From this example, we can see that the ability of the Python programming language to work together with other languages is relatively strong.


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.