MATLAB Answers

0

Can Someone help me with Parse error at function in my matlab code ?

Asked by Sai Gandham on 19 Apr 2016
Latest activity Commented on by Gabrielle Gutierrez on 12 May 2018
% Find a VISA-GPIB object.
obj1 = instrfind('Type', 'visa-gpib', 'RsrcName', 'GPIB8::1::INSTR', 'Tag', '');
% Create the VISA-GPIB object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = visa('TEK', 'GPIB8::1::INSTR');
else
fclose(obj1);
obj1 = obj1(1);
end
% Connect to instrument object, obj1.
fopen(obj1);
%// Initialize a cell array
results = {};
%// Define a function to be called when the timer fires
function timerCallback(varargin)
newresult = query(obj1,'CALCulate:SPECtrum:MARKer0:Y?');
%// Display the result (like you currently are)
disp(newresult)
%// Append the result to your results container
results{end+1} = newresult;
end
%// Then set your timer callback
t = timer('TasksToExecute', 3, ...
'Period', 30, ...
'ExecutionMode', 'FixedRate', ...
'TimerFcn', @timerCallback);
start(t)
The above is my code.
I was getting error at line function timerCallback saying that " Parse error at FUNCTION:Usage might be invalid matlab syntax ".
Can someone tell me how to fix this error?
And also I was getting warning at line results{end+1}=newresult; Saying that " The value assigned to variable results might be unused.
If possible let me know how to rectify it.
Expecting answers as early as possible. Thank you in advance !!

  0 Comments

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 19 Apr 2016
 Accepted Answer

You cannot define a function inside a script. functions can only be defined inside files whose first non-comment is "function" or "classdef".
You need to rename your existing timerCallback.m file to something else such as timer_driver.m . Then once that is done, you need to take the part of it that starts from "function" and save it to the file timerCallback.m . Then you would run the timer_driver code.

  2 Comments

Sir , It was showing me some error stating that undefined function or variable 'obj1'.
My code till fopen(obj1); is the session log from instrument control toolbox. So I didnt created it.
how can i rectify this error ?
Functions can be defined within scripts. See the documentation for declaring functions: https://www.mathworks.com/help/matlab/ref/function.html?searchHighlight=function&s_tid=doc_srchtitle

Sign in to comment.