function defintion

2 Ansichten (letzte 30 Tage)
shafagh
shafagh am 22 Aug. 2011
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 22 Aug. 2011
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 Kommentar
shafagh
shafagh am 23 Aug. 2011
if you could please explain me the correct way of defining a function.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Chirag Gupta
Chirag Gupta am 22 Aug. 2011
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 Kommentare
shafagh
shafagh am 23 Aug. 2011
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 am 23 Aug. 2011
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by