Write ".m" matlab code files to excel

32 visualizaciones (últimos 30 días)
Glen Rodrigues
Glen Rodrigues el 17 de Ag. de 2020
Comentada: Glen Rodrigues el 17 de Ag. de 2020
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

Respuesta aceptada

Dana
Dana el 17 de Ag. de 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 comentarios
Dana
Dana el 17 de Ag. de 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 el 17 de Ag. de 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 :)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Import from MATLAB en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by