Coding a program to write an M file - possible?
古いコメントを表示
Hi all,
I am writing a GUI for an existing program. This program runs an M file (currently users specify which to run manually editing the code) which contains data and some calculations - there are hundreds of these M files for different users, that contain data about them. All are different (but the same, bar the numbers...) and have been created manually over a period of time.
I am writing a GUI which will provide an interface for loading these files, but also allow a user to make a new M-file based on their product.
Is there a way, once the user has entered the data about their product in the GUI, that I can get the program to write an M-file script, for their product?
e.g User enters mass = 610, length = 1200 and program makes script and writes m = 610; l=1200; (continues for more parameters) then saves the .m file where the others are stored.
I have easily got the GUI to load an existing m-file and populate the GUI for the user to edit it but I have no idea how I can program the GUI/program to write a script based on new data.
Thanks,
Matt.
4 件のコメント
Henry Giddens
2016 年 10 月 11 日
Hi Matt,
Isn't a '.m' file essentially just a text file? If so why not just write your program to write the code to a a text file, then change the extension to '.m'?
Matt
2016 年 10 月 11 日
Matt
2016 年 10 月 11 日
採用された回答
その他の回答 (1 件)
Guillaume
2016 年 10 月 11 日
0 投票
You can of course write any type of file you want with matlab low level IO functions. However, if I were you I would change my approach.
If the only thing that changes between m files is some parameters (i.e. the actual processing code is the same), I would rewrite it so that it actually loads those parameters (from a mat file, text file, database, whatever is more practical) at start and uses these. That way the m file never changes and you distribute the configuration file to the user.
3 件のコメント
As an addendum and an answer to your comment to the question, the simplest way to write your 'm = somevalue' into a file (you can just write it straight with .m extension, no need to rename):
mass = 620; comes from your GUI
fid = fopen('C:\somewhere\filetowrite.m', 'wt'); %open file for writing (as text so line endings are correct for your platform)
fprintf(fileid, 'm = %f\n', mass); %if mass is always integer replace %f by %d
However, it would be much simpler in the long term to have the two separate files as I've suggested. It also should not affect performance at all. It's also a lot more flexible, you can actually update the processing code (e.g. fix bugs) without having to recreate all the individual user files.
Matt
2016 年 10 月 11 日
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!