Write ".m" matlab code files to excel

31 views (last 30 days)
Hi, I have currently designed a simulation tool on matlab which uses a .m input file. The file basically contains parameters and data required for simulation. The results generated are then written to excel file. What I was wondering is if it is possible to write this input file , which is a ".m" code file to excel? basically copy the contents of this m file as is and paste it to excel. Is there a functon or command I could use to do this?
Thank You In Advance

Accepted Answer

Dana
Dana on 17 Aug 2020
A few things. First of all, I want to double-check that the file you're talking about is indeed an .m file. .m files are MATLAB scripts/functions, i.e., files which contain instructions telling MATLAB to do things. .mat files, meanwhile, are files used to store MATLAB data.
If indeed it's the case that you're storing data/parameters in an .m file, then step 1 is to save that information properly to a .mat file, and step 2 is to never store data/parameters in an .m file ever again. To do step 1, load up all that info into MATLAB variables in your workspace, and then use the save command to save it to a .mat file. From then on, you just need to use the load command to load up that data in MATLAB. Step 2, meanwhile, is self-explanatory.
Now, assuming you've got your data in a .mat file, to convert to an Excel file, you'll want to read up on xlswrite.
Finally, if you literally want the text in an .m file to be copied to an Excel file, the easiest thing would be to literally just open the .m file in the editor, highlight what you want, copy, and then paste in Excel. I can't really see why you'd ever want to do something like that, but hey, stranger things have happened I guess.
  6 Comments
Dana
Dana on 17 Aug 2020
This is much easier converting to a .txt file than an Excel file. Excel files, like .mat files, are best for storing data, but you're talking about storing lines of code from an .m file, which is properly interpreted as text.
So here's what you could do:
mfilename = 'input.m'; % name of .m file you want to copy
txtfilename = 'input.txt'; % name for the .txt file you'll create (WITH FILE EXTENSION)
copyfile(mfilename,txtfilename); % tell MATLAB to copy the .m file 'mfilename'
% and name it 'txtfilename'
Now you have a copy of the .m file, but saved as a plain text file.
Glen Rodrigues
Glen Rodrigues on 17 Aug 2020
Yep, even I was thinking the same. Was just going through the support page for copyfile command and like you said sounds more reasonable to convert to text file rather than excel.
Thank you :)

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!