Showing : At least one END is missing: the statement may begin here.

조회 수: 10 (최근 30일)
Yulia M
Yulia M 2020년 2월 22일
편집: Walter Roberson 2024년 10월 7일
I am trying to run this code but getting the mentioned error:
I tried to indent the code with smart ident but it is not working either! solution suggested for this kind of error.
rng (42);
s = randi ([0, 3], 1, 100000);
dlmwrite ( 'random_4.txt' , s, ',' );
rng (42);
s = randi ([0, 1], 1, 100000);
dlmwrite ( 'random_1.txt' , s, ',' );
rng (42 * 42);
s = randn (1, 100000);
dlmwrite ( 'randn_seed4242.txt' , s, ',' );
>> create_random_data
Error: File: rng.m Line: 6 Column: 1
At least one END is missing: the statement may begin here.
Error in create_random_data (line 3)
rng (42);
  댓글 수: 6
Jacob Wood
Jacob Wood 2020년 2월 22일
편집: Jacob Wood 2020년 2월 22일
When I run the attached create_random_data.m the only error I get is this:
Error using dlmwrite (line 104)
, is not a valid attribute or delimiter. Delimiter must be a single character.
Error in Untitled (line 5)
dlmwrite('random_4.txt', s, ', ');
This is caused by the space after the commas in all your dlmwrite calls. Just leave the comma in there, dlmwrite can only use single character delimiters. The code functions properly after that edit for me.
However, your function rng() is overwriting Matlab's built-in rng() and is the reason the code is breaking for you. You either need to delete this rng.m file and use Matlab's rng(), or use proper syntax in defining your rng(). The error you are seeing is because Matlab expects to see the keyword "end" at the end of your if statements and function definitions, not "endif" or "endfunction".
Yulia M
Yulia M 2020년 2월 22일
@jacob Wood thank you it was the problem you specified it helped me a lot.

댓글을 달려면 로그인하십시오.

답변 (2개)

the cyclist
the cyclist 2020년 2월 22일
편집: the cyclist 2020년 2월 22일
The last line of rng.m is
endfunction
but it should be just
end
I also spotted another problem, which is that you should have
end % this is correct MATLAB syntax
instead of
endif % this is not correct MATLAB syntax
You are then going to encounter a new error:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in rng (line 4)
state = [state_n, state_i];
Error in create_random_data (line 3)
rng(42);
but it is not perfectly clear to me what you are trying to do there, so I suggest you try to solve that one on your own, and post a new question if you get stuck.

Manjunath
Manjunath 2024년 10월 7일
편집: Walter Roberson 2024년 10월 7일
% To find autocorrelation of the given sequence using formula
x=input('Type in the sequence: ');
maxlag=2*length(x)-1;
autocor_x=[];
lag_save=[];
len=length(x);
for lag=-(len-1):(len-1)
lag_save=[lag_save,lag];
xshifted=[];
xmodified=[];
r=0;
if(lag<=-1)
temp_lag=lag.*-1;
xshifted=[x,zeros(1,temp_lag)];
xmod=[zeros(1,temp_lag),x];
end
if(lag>=1)
xshifted=[zeros(1,lag),x];
xmod=[x,zeros(1,lag)];
end
if(lag==0)
xshifted=x;
xmod=x;
for k=1:length(xshifted) r=r+xshifted(k)*xmod (k);
end
autocor_x=[autocor_x,r];
end
disp(['The number of lags: ',num2str(maxlag)]);
disp(['The autocorrelation of given sequence:',num2str(autocor_x)]);
disp(['The lags:',num2str(lag_save)]);
stem(lag_save,autocor_x);
xlabel('Lag index');
ylabel('Amplitude')
title('Autocorrelation');
  댓글 수: 1
Steven Lord
Steven Lord 2024년 10월 7일
When I copied your code into the MATLAB Editor and smart indented the code, the for statement on line 7 does not have a corresponding end statement lined up with it. Adding that end at the end of the code eliminated the red Code Analyzer error message, though a few orange Code Analyzer warning messages remained.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Live Scripts and Functions에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by