我遇到了一个与matlab相关的案例。
我使用以下命令通过脚本创建文件:
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 CHECK按钮然后创建了f.m文件,因为它是脚本的一部分并在f.m文件中生成以下代码
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 :(得分:1)
我不会滥用日记命令。
为什么不呢:
fid = fopen('f.m','w');
fprintf(fid,'function y=f(x)\n');
fprintf(fid,'y=%s;\n';
fclose(fid);