How to use a Temporary Variable Instead In MATLAB
What we did in the previous example, can be done with the help of a temporary variable. See the code below to understand.
Matlab
% Code k = struct( 'val' ,[1 2 3 4].^2); k.val clear k %verifying that k variable is deleted disp(k) |
The above code computes the square of a given vector and stores it as a property of a struct k. Now, we can access this property using the dot indexing of structures in MATLAB and then, we delete the struct k using the clear function of MATLAB. To verify that the variable is deleted, we display it using disp(), which should throw an error.
Output:
As it can be seen, we used k as a temporary variable and deleted it once its work was done. The dot (.) indexing in function call results does the same thing without the requirement of creating any extra variables.
Indexing into Function Call Results in MATLAB
MATLAB provides the option to return an array, vector, struct, etc. as the function return value. This means that the user can return an array/vector from a function call’s result. Naturally, there is a need to access those arrays/vectors and use them further in a MATLAB workspace.
In this article, we shall see how to index function results in a MATLAB script with the help of various examples and methods.