Function does a thing for an amount of time, then another thing.

Basically, I'm trying to replicate a Closed Transfer Switch in a Matlab Function. I have two inputs (two power sources) and two outputs. For simplicities sake, I have set it up so that when the first inputs rises above a specific value, that input will then drop to 0 and input 2 will flow through output 2. However, the behaviour of Closed Transfer Switches results in a brief moment of paralleling, where, for 100ms or less, both power sources are connected, so therefore, in this case, both inputs flow through the outputs.
I have had a stab at programming a function to achieve this. I don't really understand why it doesn't seem to be working as I've used the "etime" function to determine the time in which both outputs should be flowing. This isn't working and I really don't understand why. I've used a couple of functions to try simplify things a bit for myself but again, doesn't seem to be working.
Closed Transfer Switch Function.
%Closed Transfer Switch Function
function [y1, y2] = CTS(u1, u2)
coder.extrinsic('clock')
t0=clock;
u1=0;
u2=0;
y1=0;
y2=0;
if abs(u1) > 20
[y1, y2] = Para(u1, u2, t0);
else
[y2, y1] = Para(u2, u1, t0);
end
Paralleling Function
function [out1, out2] = Para(in1, in2, s)
coder.extrinsic('clock')
coder.extrinsic('etime')
in1=0;
in2=0;
out1=0;
out2=0;
if etime(clock, s) < 0.1
out1=in1;
out2=in2;
else
out1=0;
out2=in2;
end
Does anyone know why this isn't working? Any help would be much appreciated! Thanks all for your time! Ali.

1 Comment

"Does anyone know why this isn't working?"
You replace all of the input values with zero.

Sign in to comment.

Answers (1)

In both of your functions, you're telling the user "Thanks for the inputs, but I'm gonna use 0."
MATLAB's Code Analyzer is probably telling you the exact same thing with orange squigglies under u1, u2 and in1, in2.
To try to put it one more way:
function CTS(u1,u2)
u1 = 0;
u2 = 0;
Has the same effect on u1 and u2 as:
u1 = 9;
u1 = 0;
u2 = 9;
u2 = 0;
The difference seems to be you aren't familiar with calling functions in MATLAB. You would call it from the command window(or another function/script, just like you did with Para):
CTS(9,9);
This tells CTS that u1 should initialize to 9, and u2 should initialize to 9. If you click the big green play triangle, or call it from command window as:
CTS();
You aren't initializing u1 or u2 to anything, so you'll get "Undefined Variable 'u1'" errors. -- I assume everything I said is just a re-hash of the content in the various links provided by Stephen.

5 Comments

The reason I define my variables as 0 before the 'if' function is because without them, which is how I originally programmed it, I was receiving the following error;
"Undefined function or variable 'y2'. The first assignment to a
local variable determines its class."
Looking this error up on these forums I found the following answer from someone with a similar error.
"Since all of my "Undefined function or variable" errors were on variables, and all my variables were 1 x 1 doubles, I just defined them all as 0 prior to them being defined in a for loop. It seems you can not define them in a for loop."
I was under the impression setting these variables as 0 was just initialising them as integers? No? Removing the initialisation as zero results in more errors.
MATLAB does not require that variables are "initialized" as with most compiled languages. It is required that a variable exists before you try to use it (with some exceptions, such as indexing into a variable), so if you want to concatenate or calculate with a variable then it will need to be defined before you try to use it.
In any case, as both Greg and I told you over twelve hours ago, your functions have input values which you completely ignore and replace with zeros. In your case all of those zeros are a total waste of time, and they just make your code buggy, because then the inputs then do not work. In the case of the outputs those zeros do nothing at all because inside the if statement you totally reallocate those variables.
I've just realised that I shouldn't have those 0 initialisations in my 'Para' function but potentially just in my main CTS function. I have now fixed this. However, I am still receiving the following errors highlighted when trying to execute the "etime" function;
Expected either a logical, char, int, fi, single, or double. Found
an mxArray. MxArrays are returned from calls to the MATLAB
interpreter and are not supported inside expressions. They may
only be used on the right-hand side of assignments and as
arguments to extrinsic functions
"I've just realised that I shouldn't have those 0 initialisations in my 'Para' function but potentially just in my main CTS function"
Why do you think that you need them in the function CTS? Inside CTS you reallocate the input variables to be zero, thus making the function input arguments totally useless. And allocating the output variables to be zero does nothing at all because you simply redefine those variables a few lines later. What is the point of all that?
I highly recommend that you work through the MATLAB Getting Started tutorials, which teach very basic MATLAB usage and would help you a lot:
The page I found advising initialisation of variables is linked below;
https://www.mathworks.com/matlabcentral/answers/39191-undefined-function-or-variable-the-first-assignment-to-a-local-variable-determines-its-clas
Again, I'm a complete novice programmer. The page linked above is the reason why I carried out that action, which I totally appreciate might have been incorrect. I was just trying to narrow down what my problem might actually be.
Sorry also for the delayed response. I only got the notifications when I woke this morning.
Thanks again!

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 18 Oct 2017

Edited:

on 19 Oct 2017

Community Treasure Hunt

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

Start Hunting!