Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to save and execute segments of code???

Subject: How to save and execute segments of code???

From: Sebastian

Date: 17 May, 2013 19:19:09

Message: 1 of 3

Hi,

I'm currently programming a robot in Matlab and it must do specific tasks depending the stage of the whole process it is currently in.

The thing is that, It would be wonderful to be able to save chunks of code with complex instructions for the robot to follow, index them somehow, and then call those codes from the main .m file.

For example:

instruction(1) -> Set of instructions for the first stage
instruction(2) -> Set of instructions for the second stage
... and on ...

So I can then call:

instruction(X)

where X is the stage it is currently in

I already have all the stage instructions in a separate .m file, now I just need a smart way of calling the different segments

Thank you!!!

Subject: How to save and execute segments of code???

From: Steven_Lord

Date: 17 May, 2013 21:02:02

Message: 2 of 3



"Sebastian " <seba.rivas@gmail.com> wrote in message
news:kn5vrd$9jo$1@newscl01ah.mathworks.com...
> Hi,
>
> I'm currently programming a robot in Matlab and it must do specific tasks
> depending the stage of the whole process it is currently in.
>
> The thing is that, It would be wonderful to be able to save chunks of code
> with complex instructions for the robot to follow, index them somehow, and
> then call those codes from the main .m file.
>
> For example:
>
> instruction(1) -> Set of instructions for the first stage
> instruction(2) -> Set of instructions for the second stage
> ... and on ...
>
> So I can then call:
>
> instruction(X)
>
> where X is the stage it is currently in
>
> I already have all the stage instructions in a separate .m file, now I
> just need a smart way of calling the different segments

That's what functions are for. You could store function handles to the
various functions in a cell array and call them by extracting the
appropriate element.

trig = {@cos, @sin, @tan}
sineFun = trig{2}
sineOf3 = sineFun(3)
forComparison = sin(3)

See the documentation on function handles for more information.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: How to save and execute segments of code???

From: Sebastian

Date: 17 May, 2013 22:50:09

Message: 3 of 3

"Steven_Lord" <slord@mathworks.com> wrote in message <kn65s9$sbi$1@newscl01ah.mathworks.com>...
>
>
> "Sebastian " <seba.rivas@gmail.com> wrote in message
> news:kn5vrd$9jo$1@newscl01ah.mathworks.com...
> > Hi,
> >
> > I'm currently programming a robot in Matlab and it must do specific tasks
> > depending the stage of the whole process it is currently in.
> >
> > The thing is that, It would be wonderful to be able to save chunks of code
> > with complex instructions for the robot to follow, index them somehow, and
> > then call those codes from the main .m file.
> >
> > For example:
> >
> > instruction(1) -> Set of instructions for the first stage
> > instruction(2) -> Set of instructions for the second stage
> > ... and on ...
> >
> > So I can then call:
> >
> > instruction(X)
> >
> > where X is the stage it is currently in
> >
> > I already have all the stage instructions in a separate .m file, now I
> > just need a smart way of calling the different segments
>
> That's what functions are for. You could store function handles to the
> various functions in a cell array and call them by extracting the
> appropriate element.
>
> trig = {@cos, @sin, @tan}
> sineFun = trig{2}
> sineOf3 = sineFun(3)
> forComparison = sin(3)
>
> See the documentation on function handles for more information.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


That doesn't really work for me since each instruction set has over 30 instructions, and I've got over 40 instruction sets... As you'd imagine, making a function for each would be incredibly troublesome.

Instead I already figured out how to do this... I just made a function out of the large set of instructions, and separated the sets with IF blocks, so depending on the input parameter, a single block is selected.

Thanks though!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us