function defintion

1 次查看(过去 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 中查找有关 Debugging and Analysis 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by