From: jia on 4 Aug 2010 15:58 Hello: I hope to create a function as follows, but it says it is invalid. The output is a struct data type. Does anyone know the reason? function [act(Num).dur,act(Num).resType] = Creact(Num,actDuration, resType) act(Num).dura=actDuration act(Num).resType=resType end Thanks,
From: us on 4 Aug 2010 16:03 On Aug 4, 9:58 pm, "jia " <kuku...(a)gmail.com> wrote: > Hello: > > I hope to create a function as follows, but it says it is invalid. The output is a struct data type. Does anyone know the reason? > > function [act(Num).dur,act(Num).resType] = Creact(Num,actDuration, resType) > act(Num).dura=actDuration > act(Num).resType=resType > end > > Thanks, you cannot define output args the way you do it... one of the many solutions - this depends on the context of your function... function act=Creact(Num,actDuration, resType) % your code end us
From: Nathan on 4 Aug 2010 16:04 On Aug 4, 12:58 pm, "jia " <kuku...(a)gmail.com> wrote: > Hello: > > I hope to create a function as follows, but it says it is invalid. The output is a struct data type. Does anyone know the reason? > > function [act(Num).dur,act(Num).resType] = Creact(Num,actDuration, resType) > act(Num).dura=actDuration > act(Num).resType=resType > end > > Thanks, Does returning just act work? For example: function [act] = Creact(Num,actDuration, resType) act(Num).dura=actDuration act(Num).resType=resType end Outside of this function you should be able to access the just- created .dura and .resType as such: num = randi(10) %some number actDuration = rand %some duration resType = rand %whatever you want... a = Creact(num,actDuration,resType) a(num).dura a(num).resType Without testing it myself, that looks like it should work... -Nathan
From: jia on 4 Aug 2010 16:19 Thank you both! Nathan <ngreco32(a)gmail.com> wrote in message <ed7ad5f9-de1c-4b82-a8a7-cbfe8a6fd2e3(a)z34g2000pro.googlegroups.com>... > On Aug 4, 12:58 pm, "jia " <kuku...(a)gmail.com> wrote: > > Hello: > > > > I hope to create a function as follows, but it says it is invalid. The output is a struct data type. Does anyone know the reason? > > > > function [act(Num).dur,act(Num).resType] = Creact(Num,actDuration, resType) > > act(Num).dura=actDuration > > act(Num).resType=resType > > end > > > > Thanks, > > Does returning just act work? For example: > > function [act] = Creact(Num,actDuration, resType) > act(Num).dura=actDuration > act(Num).resType=resType > end > > Outside of this function you should be able to access the just- > created .dura and .resType as such: > > num = randi(10) %some number > actDuration = rand %some duration > resType = rand %whatever you want... > > a = Creact(num,actDuration,resType) > > a(num).dura > a(num).resType > > Without testing it myself, that looks like it should work... > > -Nathan
From: Steven_Lord on 5 Aug 2010 10:13 "jia " <kukuzry(a)gmail.com> wrote in message news:i3cgoc$bct$1(a)fred.mathworks.com... > Hello: > > I hope to create a function as follows, but it says it is invalid. The > output is a struct data type. Does anyone know the reason? > function [act(Num).dur,act(Num).resType] = Creact(Num,actDuration, > resType) > act(Num).dura=actDuration > act(Num).resType=resType > end > > Thanks, Others have told you how to correct your code, but your original question was why this doesn't work. The input arguments and output arguments you specify on your function declaration line MUST be valid variable names.[1] act(Num).dur and act(Num).resType are NOT variable names; they are expressions, and so cannot be included as a function argument in a function declaration. [1] There is one exception to this if you're using release R2009b or later, and that is when tilde (~) when used as an input argument. See the following section of the Release Notes for more information on this special case if you're interested. http://www.mathworks.com/access/helpdesk/help/techdoc/rn/br5fo8o-1.html#br65zmd-1 -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ To contact Technical Support use the Contact Us link on http://www.mathworks.com
|
Pages: 1 Prev: Optimization of a loop Next: Really need help to speed up piece of code |