Coding a program to write an M file - possible?

44 次查看(过去 30 天)
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 个评论
Matt
Matt 2016-10-11
I suppose I could use something like
fprintf(['m = ' num2str(mass)]..........etc.)
Matt
Matt 2016-10-11
编辑:Matt 2016-10-11
This concept works (to edit product, could easily write new file for new product) but as said below perhaps I need to reconsider the approach...
% Specify a Variable
mass = 610
% Open Text File (could be .m - rename to .txt first)
fileID = fopen('client1produt1.txt','w');
% Edit Text File
fprintf(fileID,['m = ' num2str(mass)]);
% Close Text File
fclose(fileID);
% Rename File to Create M File
movefile('client1produt1.txt','client1produt1.m', 'f')
% Run Created File to Check Working
client1produt1

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2016-10-11
I'd do like Guillaume suggests. Have a "main" m-file, with a constant name that reads in the mat file, which has a name where the parameters are coded into it and into the filename. So you'd do something like
% Create mat file
baseFileName = sprintf('m%d, l%d.mat', m, l);
fullMatFileName = fullfile(folder, baseFileName);
save(fullMatFileName, 'm', 'l');
Now, in main.m:
% Get the mat file they've been given
filePattern = fullfile(folder, 'm*.mat');
files = dir(filePattern); % Hopefully returns only 1, but could return more if there are more.
thisMatFileName = fullfile(files(1).folder, files(1).name); % Get first name
% Open it
storedStructure = load(thisMatFileName);
% Extract m and l
m = storedStructure.m;
l = storedStructure.l;
Of course if they have been given more than one .mat file, you should ask them which one they want to use. I'd call uigetfile() for that, or you could put the filenames into a cell array and use menu().
  2 个评论
Matt
Matt 2016-10-11
This is by far the best solution, as Guillaume suggested.
Thanks very much for your example - I'll have a play to familiarise myself with the approach, then try to implement it.
Deep down I knew it was the best solution, but thought it was trickier to implement than it seems.
All is not lost, i've just taught myself how to write a text file, very handy, and can now make a new script using GUI data - may be useful one day.
Matt
Matt 2016-10-21
Thanks - based on this and some other recent questions I have revamped my program to save and load data from .mat files instead of .m files.
Now I no longer need to use the run command to run .m files in other folders, nor write an .m file when a user adds details for a new product, and my data handling is much better.
It's not quite done, but I think i'm almost there!
Thanks very much!

请先登录,再进行评论。

更多回答(1 个)

Guillaume
Guillaume 2016-10-11
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 个评论
Guillaume
Guillaume 2016-10-11
编辑:Guillaume 2016-10-11
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
Matt 2016-10-11
Thanks very much - deep down I knew it was the best solution, but thought it was trickier to implement than it seems.
RE writing a script - I managed to do something similar, as seen in my comment on my original post. Handy to know that you can write .m directly, thanks.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Platform and License 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by