writing mfile using a mfile

Can any one pls give an example on how to write into an mfile using a mfile i.e mscript ??

Réponses (2)

Jan
Jan le 4 Oct 2011

0 votes

This is not complicated:
FID = fopen(fullfile(tempdir, 'MyFunction.m'));
if FID < 0, error('Cannot open file'); end
fprintf(FID, 'function out = MyFunction(in)\n');
fprintf(FID, 'disp(in);\n');
fprintf(FID, 'out = clock;\n');
fclose(FID)
But creating M-files dynamically is a critical task. Are you really sure, that this is an efficient method to solve your problem?

6 commentaires

Rajan
Rajan le 4 Oct 2011
Thank you for reply Jan Simon.
I get a error saying:Function is not defined for 'cell' inputs.
when i run this:
a1={'Apple','Ball','cat','Dog'};
fid = fopen('created.m', 'w');
fprintf(fid, 'Alphabets\n');
fprintf(fid, '%s \n',a1(1)); %This line is giving the error %
fclose(fid);
type created.m
How can i print all the elements in a1 in my mfile.
Walter Roberson
Walter Roberson le 4 Oct 2011
fprintf(fid, '%s \n',a1{1});
Notice the {} instead of ()
Matt Tearle
Matt Tearle le 4 Oct 2011
fprintf(fid, '%s \n',a1{:});
for extra vectorized bonus points :)
Rajan
Rajan le 4 Oct 2011
Oh i missed that Thank you for the answer.
how can i print it as 'Apple' not just as : Apple
Matt Tearle
Matt Tearle le 4 Oct 2011
With the single quote marks actually included, you mean?
fprintf(1, '''%s'' \n',a1{:});
Rajan
Rajan le 4 Oct 2011
yea Matt figured it out.thank you for the comments.

Connectez-vous pour commenter.

Matt Tearle
Matt Tearle le 4 Oct 2011

0 votes

2 commentaires

Rajan
Rajan le 4 Oct 2011
Thank you for the answer.
I am using R2009b.
Do u have any idea of how to do it in R2009b?
Jan
Jan le 5 Oct 2011
Editor = com.mathworks.mlservices.MLEditorServices;
methods(Editor)

Connectez-vous pour commenter.

Catégories

Tags

Question posée :

le 4 Oct 2011

Community Treasure Hunt

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

Start Hunting!

Translated by