00:01
We want to write a matlab function called checkinputs, which checks if the three input parameters are decorated at types.
00:15
The first input variable is a string type, the second input variable is a numeric type and the third input variable is a logical type.
00:30
The output of this function should be a logical row array called errorcode with four values.
00:40
If all tests pass, then all four entries in errorcode array are false.
00:48
The first input parameter to the function checkinputs is not a string, so if the first input parameter, which is supposed to be a string, is not a string, then the first array element in errorcode is set to true, otherwise is false, as is when all is correct.
01:16
The second input parameter is not numeric, means we should put the second array in errorcode to true.
01:27
If the third input parameter is not a logical, then the third array element in errorcode is set to true.
01:34
So, in summary, what we set true at the corresponding position of the parameter in the output array errorcode, we put true when the condition is not satisfied, that is, when the corresponding parameter is not of the expected type.
01:54
And the fourth array element in errorcode is set true if three input parameters are not present.
02:02
So, if we pass the call to the function, we pass only one or two parameters, then this fourth array element in errorcode is set to true, otherwise is false.
02:16
And so we can take advantage of, because we have to check the data types in the well -known functions ischart, isnumeric and islogical of matlab, we take advantage of the fact that those functions return logical values.
02:35
So let's see first the construction of this function, which as you can see is a very short solution.
02:44
So, we have the checkinput function, the return is errorcode, which happens to be an array, but here we don't know that already.
02:55
And then we have three parameters in the definition of the function, name, gpa and enrollstatus.
03:02
So we initialize the errorcode output array to false, and we can do that in matlab using this instruction false, 1, 4 indicating it's one row, four columns.
03:18
So we are following the indications here that the output is a logical row array with four values.
03:29
So this row array means it has only one row, and four values mean four columns, and issubtype logical and with this instruction false, 1, 4 we are creating a 1 times 4 array of logicals all equal to false, or initialized to false.
03:53
There are other ways you can do that, but this seems to be the better way in the sense that we are using the logical false type all at once here.
04:04
We are not transforming numerical arrays into logicals to do that.
04:11
You can of course create a numerical array of zeros, of one row and four columns, and then transform that to logicals in the logical function.
04:21
But this is a more natural way of creating an array of all entries false, and you give the dimensions of the array.
04:32
Of course, after this initialization, you can modify any element of the array to set it to a different value.
04:40
Okay, after that initialization, indeed, i can remark that this initialization here, before doing any other thing in the program, is convenient in the sense that if we have a return somewhere inside the program, then there is some warnings that matlab gives us in the command window when we haven't initialized the output array or output variable, and we have a return without doing that initialization.
05:15
So doing this initialization here ensures that there won't be any warnings about that.
05:23
Now we check for the number of input arguments to the function when we are running the function, when we are calling the function.
05:33
And that is done by the variable nargin.
05:39
Nargin is a variable that is put equal to the number of input parameters that we pass to the function at the time of calling the function.
05:53
For example, in another function or in the command window.
06:00
So when we call the function checkinputs and we pass one or two input parameters, then this nargin will be one or two.
06:11
So we are checking if it's not three, which is what we want, then we put the fourth entry of error code array, the output array, the fourth entry equal to true.
06:25
And we return.
06:26
That means that the array is going to be, in that case, all zeros in the first three entries and one in the last entry.
06:34
Remember those zeros and one i'm referring here are logical values...