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:
condition

Subject: condition

From: Pink Panther

Date: 19 Aug, 2010 15:55:21

Message: 1 of 3

hi everybody
in my program I have to test for a certain number of conditions. these are not complex conditions, but as I will to test a number of them, replacing one by another, I wish I could isolate them. To be a bit more specific , let's take two vectors
A(25,1)
B(25,1)
the condition would be like
C1(i) = function1(A(i),B(i)) < function2(A(i),B(i))
so C1(i) is true if function1(i) < function2(i)

so the idea would be to define C1(i) as en entry to the program, and then execute something like

for i = 1:25
   if C(i) = 1
     then ...
     else ...
   end
end

As the conditions to be tested should not be long and complex ones, I am trying to avoid creating a function in a separate m file and call that function : it looks a bit of an overkill.
Is it possible at all to do something like that? hopefully the question is clear... thanks in advance.

Subject: condition

From: Darren Rowland

Date: 20 Aug, 2010 01:42:08

Message: 2 of 3

PP,

The question isn't totally clear but I'll give it a shot.
If you don't want to create a separate m-file for each of your function1, function2 etc. then you might want to place all the functions in a single m-file, i.e.

function argout = main(argin)

% call function1, function2 etc.

end

function argout = function1(argin)
% compute function 1
end

function argout = function2(argin)
%
end
etc.

Then the sub-functions are isolated and can only be called from within main.
Hth,
Darren

Subject: condition

From: Walter Roberson

Date: 20 Aug, 2010 18:16:15

Message: 3 of 3

On 10-08-19 08:42 PM, Darren Rowland wrote:
> PP,
>
> The question isn't totally clear but I'll give it a shot.
> If you don't want to create a separate m-file for each of your
> function1, function2 etc. then you might want to place all the functions
> in a single m-file, i.e.
>
> function argout = main(argin)
>
> % call function1, function2 etc.
>
> end
>
> function argout = function1(argin)
> % compute function 1
> end
>
> function argout = function2(argin)
> %
> end
> etc.
>
> Then the sub-functions are isolated and can only be called from within
> main.
> Hth,
> Darren

For a switch-yard,

function [x, y, z] = mymain(p, q, r, s, t, u)
   if ischar(p) && isequal(p, 'gethandles')
     x = {@subfunction1, @subfunction2};
     y = [];
     z = [];
     return
   end

   ...
end

function subfunction1(....)
....
end

function subfunction2(....)
....
end



To use:

mainfunctions = main('gethandles');
mainfunctions{2}(arg1, arg2, ......); %call the function in mymain

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