Coding a program to write an M file - possible?
42 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Image Analyst
on 11 Oct 2016
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().
More Answers (1)
Guillaume
on 11 Oct 2016
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 Comments
Guillaume
on 11 Oct 2016
Edited: Guillaume
on 11 Oct 2016
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.
See Also
Categories
Find more on Platform and License in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!