2011-11-10 19 views
0

matlabに関連する1つのケースに詰まっています。実行後にMATLABのmファイルから行を削除するには

私は、これらのコマンドを使用して、スクリプトによるファイル作成:

delete output 
delete f.m 
clc; 
% This is a Cell Array! 
prompt={'Enter Function f(x) Here :'};... 
% Name of the Dialog Box 
name='Trapezoidal Rule Input Screen';... 
numlines=1; % Number of lines visible for User Input 
% Default Answer 
defaultanswer={'1 + exp(-x).*sin(4*x)'};... 
% Creating the Dialog box where the User Input is stored into a Cell Array 
answer=inputdlg(prompt,name,numlines,defaultanswer);... 
global y1 
y1=answer{1};... 
diary f.m; 
disp('function y = f(x)');... 
      %disp('y = '),disp(y),disp(';');... 
      %disp('y = ((1+(x^2))^-1);');... 
      fprintf('y=%s;\n',y1); 
diary off; 
f(0); 

RUNのチェックボタンをクリックし、次にFMファイルは、それがスクリプトの一部であるために作成され、FMファイルに次のコードを生成していた

function y = f(x) 
      %disp('y = '),disp(y),disp(';');... 
      %disp('y = ((1+(x^2))^-1);');... 
      fprintf('y=%s;\n',y1); 
y=1 + exp(-x).*sin(4*x); 
diary off; 

しかし、それが定義される前にy1が使用されていることを停止して強調表示するエラー。

fprintf('y=%s;\n',y1); 

この問題に関するいくつかの解決策がある場合は、私を助けてください。

+0

なぜあなたはすべてでf.mを作成するのですか?オリジナルのスクリプトでfmの内容をしないのはなぜですか? –

+0

エディタの '{}'ボタンを使ってコードをフォーマットしてください。 – Mat

答えて

1

私はこのために日記のコマンドを乱用しません。

理由だけではない:

fid = fopen('f.m','w'); 
fprintf(fid,'function y=f(x)\n'); 
fprintf(fid,'y=%s;\n'; 
fclose(fid); 
関連する問題