【问题标题】:In Matlab I keep getting undefined variable or function error despite clearly defining the variable在 Matlab 中,尽管明确定义了变量,但我不断收到未定义的变量或函数错误
【发布时间】:2016-11-19 03:45:38
【问题描述】:

我在 Matlab 中有这段代码,它应该采用翼型轮廓并增加点数,这样当我在另一个程序中绘制轮廓时,我会得到更平滑的曲线。

clear
%reading an external data file
fid = fopen('NACA0015.txt');
a = fscanf(fid,'%g %g',[2 inf]); % It has two rows now.
a = a';    % matrix transpose

n = input('200')      %e.g., n=35

for i=1:n
    for j=1:2
      fprintf('%12.7f',a(i,j)); %a(i,1) is first column, a(i,2) is 2nd col
    end
    fprintf('\n');
end
fclose(fid);

for i=1:n
    x(i)=a(i,1);      %x , y vectors
    y(i)=a(i,2);
end

% use spline to create more points

xx=0:0.01:1      % e.g., step =0.01 (number of points = (1-0)/0.01=100)
yy = spline(x,y,xx);      % xx and yy are new generated values of Naca0012

fprintf('\n print spline values \n');

plot(xx,yy,'ro')    
hold on
plot(x,y,'*')

当我运行它时,我得到了错误

未定义的函数或变量“x”。

读取_external_data_and_spline 时出错(第 26 行) yy = 样条(x,y,xx); % xx 和 yy 是 Naca0012 的新生成值

当 x 变量在代码中明确定义时,我完全不知道为什么这不起作用,请有人帮我解决这个问题

【问题讨论】:

  • 我猜n0,因此创建x 的循环永远不会运行,因为1:0 会产生一个空数组。
  • 此外,如果您在不提供输入的情况下按 Enter 键,input 会返回一个空矩阵。尝试从1:[] 迭代意味着x 永远不会被初始化,与1:(any number less than 1) 相同。
  • 我认为这不是问题,因为当我运行它时,它会在命令控制台中显示 200,然后我按 Enter 键,因此已提供输入
  • 但是如果你在那行之后设置一个断点并查看n的值,你会得到什么?

标签: matlab spline


【解决方案1】:

这就是您使用input 的方式。 input 中的参数不是默认值,而是提示文本。如果你在控制台输入命令并回车,你会得到:

>> n = input('200')
200

n =

     []

>> 

输入不接受默认值。如果你真的想要一个带有默认答案的交互式提示,你需要inputdlg

answer = inputdlg('Enter a number of lines to parse', 'n', 1, '200');
n = str2double(answer);

请注意,inputdlg 总是返回文本,因此您需要转换为数字。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-14
    • 2015-02-22
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 2023-03-08
    • 2013-09-27
    • 1970-01-01
    相关资源
    最近更新 更多