Write ".m" matlab code files to excel

32 次查看(过去 30 天)
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

采纳的回答

Dana
Dana 2020-8-17
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 个评论
Dana
Dana 2020-8-17
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 2020-8-17
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 :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by