Matlab 此语句不在任何函数内
我在第51行得到一个解析错误(以startw=input(“输入起始波长:”)开始),但我不知道为什么。错误是这样读的;第51行第1列:此语句不在任何函数内。(在结束函数“OpticalFunction”的定义之后。)当在Matlab脚本上运行时,所讨论的行本身工作得非常好Matlab 此语句不在任何函数内,matlab,function,daq-mx,Matlab,Function,Daq Mx,我在第51行得到一个解析错误(以startw=input(“输入起始波长:”)开始),但我不知道为什么。错误是这样读的;第51行第1列:此语句不在任何函数内。(在结束函数“OpticalFunction”的定义之后。)当在Matlab脚本上运行时,所讨论的行本身工作得非常好 function OpticalFunction daq.reset clear, close all clc; s = daq.createSession('ni'); % Creates the session obj
function OpticalFunction
daq.reset
clear, close all
clc;
s = daq.createSession('ni');
% Creates the session object
s.addDigitalChannel('Dev1','Port0/Line0:7','OutputOnly');
% Adds 8 digital output channels (numbered 0:7) on the DAQ card
% The following creates the uicontrols
onoff = uicontrol('Style','togglebutton','String','go',...
'Position',[20 200 70 40],'Callback',@move_buggy);
forwards = uicontrol('Style','pushbutton','String','forwards',...
'Position',[20 150 70 40],'Callback',@go_forward);
backwards = uicontrol('Style','pushbutton','String','backwards',...
'Position',[20 100 70 40],'Callback',@go_backward);
nout = [51 102 204 153]; % decimal sequence for forward motion
% This is the callback function for the toggle button.
% It moves the buggy when the toggle button is pressed.
% 'hObject' is the handle for the uicontrol calling the function.
function move_buggy(hObject,eventdata)
while hObject.Value == hObject.Max
for n=1:4
output_data=dec2binvec(nout(n),8);
% high state=1 low state=0
outputSingleScan(s,output_data);
% outputs the data in output_data to the device
pause(1.6)
% use this to change the speed of the motor
end
end
end
% These are the callbacks for the pushbuttons.
% They set the direction of travel for the motors.
function go_forward(hObject,eventdata)
nout = [51 102 204 153];
end
function go_backward(hObject,eventdata)
nout = [153 204 102 51];
end
end
%%
startw = input('Enter starting wavelength: ');
deend = input('Desired final wavelength: ');
r = 11/62; % this is the rate of wavelegth change with time for GaAs
r = 29.5/66; %this is the rate of wavelenght change with time for GaP
% comment off the r value not used
OpticalFunction
% calls on the function optical thing
错误消息非常清楚:它是一条不在函数中的语句。当M文件中的第一条语句是
function
时,它是一个函数M文件,它定义了一个函数,并且不能包含函数之外的任何语句
如果您想要一个脚本M文件,您需要将脚本本身放在文件的顶部,并且必须在底部定义任何本地函数
这与Octave的工作方式完全不同,Octave的函数定义必须在使用它的脚本行之前。MATLAB和Octave不能共享定义本地函数的脚本。解决方案是在单独的文件中定义函数 对不起,如果这是一个简单的问题,我不太擅长Matlab,你的回答是非常翔实的,谢谢你。我还有一个问题,当我在底部创建一个本地函数时,它应该与它所在的文件具有相同或不同的名称。谢谢你advance@Harry:本地函数可以有您想要给它们的任何名称。它们将对其他地方定义的函数进行阴影处理,因此如果调用本地函数“size”,则该脚本文件中将不再提供正常的
size
函数。