function defintion
조회 수: 2 (최근 30일)
이전 댓글 표시
hi may be some body can correct me ? well i have matlab 2011 and try to define a function but the as i define the function as i get the remark that
??? Error: File: raman.m Line: 6 Column: 1 Function definitions are not permitted in this context.
i have no idea why
;clear;
Rd=load('A1.txt')
plot(Rd);
hold on;
[m,n]=size(Rd);
function [r] =uigetfile(Rd)
for i=1:m
y=(1/2*pi)*(w(i)^2)/(x-x(i))^2+w(i)^2
end
end
댓글 수: 0
채택된 답변
Walter Roberson
2011년 8월 22일
Naming your own function as "uigetfile" is not a good idea. You are going to greatly confuse anyone who tries to read your code.
You are going to have difficulties because your function declares that it computes a result named "r", but you do not in fact compute that result.
The result you do compute, "y", you throw away -- local variables are not saved when the function exits.
You also have a problem because at each iteration of your loop, you overwrite the same "y".
You have another problem because your function relies on "x" and "w", but neither of those are defined at the time of execution.
And of course you have the problem that although you define the function, you never call it.
추가 답변 (1개)
Chirag Gupta
2011년 8월 22일
You cannot define MATLAB functions in the middle of a script.
function myscript
clear;
Rd=load('A1.txt')
plot(Rd);
hold on;
[m,n]=size(Rd);
function [r] =uigetfile(Rd)
for i=1:m
y=(1/2*pi)*(w(i)^2)/(x-x(i))^2+w(i)^2
end
end
댓글 수: 2
Walter Roberson
2011년 8월 23일
Just like Chirag shows. Your sticking point at the moment is that it is not allows to define a function in the middle of a script. A "script" in MATLAB is a code file whose first non-comment line does *not* start with the word "function". Chirag's version DOES start with "function", and so is a MATLAB Function file rather than a MATLAB "script".
Of course once you have that issue solved you will need to solve the other issues that I mentioned in my response.
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!