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

4 Ansichten (letzte 30 Tage)
% 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 !!

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 19 Apr. 2016
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 Kommentare
Sai Gandham
Sai Gandham am 19 Apr. 2016
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 ?
Gabrielle Gutierrez
Gabrielle Gutierrez am 12 Mai 2018
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by