function defintion

조회 수: 2 (최근 30일)
shafagh
shafagh 2011년 8월 22일
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

채택된 답변

Walter Roberson
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
shafagh
shafagh 2011년 8월 23일
if you could please explain me the correct way of defining a function.

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

추가 답변 (1개)

Chirag Gupta
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
shafagh
shafagh 2011년 8월 23일
how do i that ( define the function ) then? i m new and try to learn matlab thats why i cant know it !
Walter Roberson
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 CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by