I get a parse error on line 51 (starting with startw = input('Enter starting wavelength: ')) but I have no idea why. The error reads as such; Line: 51 Column: 1 This statement is not inside any function. (It follows the END that terminates the definition of the function "OpticalFunction".) The line in question when run on a Matlab script by itself works perfectly fine.
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
The error message is pretty clear: it’s a statement that is not inside a function. When the first statement in an M-file is
function
, it is a function M-file, it defines a single function and cannot contain any statements outside functions.If you want to have a script M-file, you need to put the script itself at the top of the file, and any local functions must be defined at the bottom.
This is strictly different from how Octave does it, where the function definition must come before the script line that uses it. MATLAB and Octave cannot share scripts that define local functions. The solution is to define the functions in separate files.