Please help! "Error: Function definitions are not permitted in this context. "

Hello! I'm extremely new to Matlab, and I'm working on a homework problem, and I keep coming up with an error... I've written my functions.
When I run my function it give some error please help me to sole this error .
Error is "Functions definition are not primitted in this context"
function CAcode = generateCAcode(PRN)
% generateCAcode.m generates one of the 32 GPS satellite C/A codes.
%
% CAcode = generateCAcode(PRN)
%
% Inputs:
% PRN - PRN number of the sequence.
%
% Outputs:
% CAcode - a vector containing the desired C/A code sequence
% (chips).
%--------------------------------------------------------------------------
% SoftGNSS v3.0
%
% Copyright (C) Darius Plausinaitis
% Written by Darius Plausinaitis
% Based on Dennis M. Akos, Peter Rinder and Nicolaj Bertelsen
%--------------------------------------------------------------------------
%This program is free software; you can redistribute it and/or
%modify it under the terms of the GNU General Public License
%as published by the Free Software Foundation; either version 2
%of the License, or (at your option) any later version.
%
%This program is distributed in the hope that it will be useful,
%but WITHOUT ANY WARRANTY; without even the implied warranty of
%MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%GNU General Public License for more details.
%
%You should have received a copy of the GNU General Public License
%along with this program; if not, write to the Free Software
%Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
%USA.
%--------------------------------------------------------------------------
%CVS record:
%$Id: generateCAcode.m,v 1.1.2.5 2006/08/14 11:38:22 dpl Exp $
%--- Make the code shift array. The shift depends on the PRN number -------
% The g2s vector holds the appropriate shift of the g2 code to generate
% the C/A code (ex. for SV#19 - use a G2 shift of g2s(19) = 471)
g2s = [ 5, 6, 7, 8, 17, 18, 139, 140, 141, 251, ...
252, 254, 255, 256, 257, 258, 469, 470, 471, 472, ...
473, 474, 509, 512, 513, 514, 515, 516, 859, 860, ...
861, 862 ... end of shifts for GPS satellites
... Shifts for the ground GPS transmitter are not included
... Shifts for EGNOS and WAAS satellites (true_PRN = PRN + 87)
145, 175, 52, 21, 237, 235, 886, 657, ...
634, 762, 355, 1012, 176, 603, 130, 359, 595, 68, ...
386];
%--- Pick right shift for the given PRN number ----------------------------
g2shift = g2s(PRN);
%--- Generate G1 code -----------------------------------------------------
%--- Initialize g1 output to speed up the function ---
g1 = zeros(1, 1023);
%--- Load shift register ---
reg = -1*ones(1, 10);
%--- Generate all G1 signal chips based on the G1 feedback polynomial -----
for i=1:1023
g1(i) = reg(10);
saveBit = reg(3)*reg(10);
reg(2:10) = reg(1:9);
reg(1) = saveBit;
end
%--- Generate G2 code -----------------------------------------------------
%--- Initialize g2 output to speed up the function ---
g2 = zeros(1, 1023);
%--- Load shift register ---
reg = -1*ones(1, 10);
%--- Generate all G2 signal chips based on the G2 feedback polynomial -----
for i=1:1023
g2(i) = reg(10);
saveBit = reg(2)*reg(3)*reg(6)*reg(8)*reg(9)*reg(10);
reg(2:10) = reg(1:9);
reg(1) = saveBit;
end
%--- Shift G2 code --------------------------------------------------------
%The idea: g2 = concatenate[ g2_right_part, g2_left_part ];
g2 = [g2(1023-g2shift+1 : 1023), g2(1 : 1023-g2shift)];
%--- Form single sample C/A code by multiplying G1 and G2 -----------------
CAcode = -(g1 .* g2);
end

6 件のコメント

Adam Danz
Adam Danz 2018 年 11 月 16 日
What version of Matlab are you using (type version in the command window)?
I do not get that error message in 2018a nor 2016a. One possibility is that this function is not in its own file. Is that the case?
per isakson
per isakson 2018 年 11 月 16 日
編集済み: per isakson 2018 年 11 月 16 日
I failed to reproduce the error you see. Your function runs nicely. Please, more detailes.
Image Analyst
Image Analyst 2018 年 11 月 16 日
Please attach all relevant files with the paper clip icon.
And please attach the complete error message. I know "Functions definition are not primitted in this context" is not it, because of the misspelling. Paste all the red text, including line number and line of source code it complains about.
And, is there a little arrow that points to the place in the code where it thinks the problem originated?
Saiher Iqbal
Saiher Iqbal 2018 年 11 月 16 日
I Just upload the screenshot of the error you can checkScreenshot (26).png
Image Analyst
Image Analyst 2018 年 11 月 16 日
We can't see the crucial first line of the window. Again Please attach all relevant files with the paper clip icon.
Saiher Iqbal
Saiher Iqbal 2018 年 11 月 16 日
Here I attached the file and the error appear in MATLAB window is
function CAcode = generateCAcode(PRN)
Error: Function definitions are not permitted in this context.

サインインしてコメントする。

 採用された回答

Adam Danz
Adam Danz 2018 年 11 月 16 日
編集済み: Adam Danz 2018 年 11 月 16 日
I see what's wrong in your screen shot. You're entering the entire function in the command window. That's not how you run functions. The function should be saved to an m file named after the function (generateCAcode.m) and then you call the function from the command window.
CAcode = generateCAcode(PRN)

2 件のコメント

Saiher Iqbal
Saiher Iqbal 2018 年 11 月 16 日
Thank you so much Adam Danz
Adam Danz
Adam Danz 2018 年 11 月 16 日
No problem.

サインインしてコメントする。

その他の回答 (1 件)

Saiher Iqbal
Saiher Iqbal 2018 年 11 月 16 日

0 投票

Adam Danz I'm using 2016a version and file save in .m format....

カテゴリ

ヘルプ センター および File ExchangeMATLAB Coder についてさらに検索

製品

リリース

R2016a

タグ

質問済み:

2018 年 11 月 16 日

コメント済み:

2018 年 11 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by