Indexing into Function Call Results in MATLAB
Last Updated :
28 Apr, 2025
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.
The Dot (.) Indexing
This method of dot indexing is similar to indexing a function from a class definition. We first call a given function with required parameters and then, index its various local variables (properties) using the . indexing. See the syntax of the same below:
function out = square(var)
out = struct("prop", var.^2);
end
square(x).prop;
Here, we used dummy function that calculates the square of any given number or vector. When the function is called, we then use the (.) indexing and get the value of the square which is stored as a key : value pair in a struct.
This method can be used when you do not want to want to create unnecessary output variables. This dot indexing will directly extract the required value from the temporary variable prop, which was created by the function call and thus, is not stored in the MATLAB workspace. Now, let us see some examples of the same.
Example 1
We shall use the square function as given in the syntax above and illustrate it with a specific case.
Matlab
% Code
square([1,2,3,4]).val
%function
function k = square(var)
%creating a struct to access the value as a property
k = struct('val',var.^2);
end
Here, we create a square function which computes the square of a vector and instead of accessing it by the traditional storing in variable way, we use the function call indexing (dot indexing) to obtain the value.
Output:
Here, we obtained the square of passed vector without storing it into any variable.
The working of the above code can be understood from another approach where we use a variable to hold the value temporarily, let us see how.
Using a Temporary Variable Instead
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.
Conclusion
This article discussed how to perform indexing into function call results in MATLAB using the dot (.) indexing method and explained the same with the help of an example. We also compared the same with the help of another example, illustrating the stepwise execution of the same dot(.) indexing method.
Similar Reads
Parse Function Inputs in MATLAB Basically, the parse function is a method that users use in the function to convert data from one format to another. In programming, parsing is the processing and analysis of strings of numbers. We can also say that the parse function is often used to convert strings to other data types such as numb
4 min read
Function with Multiple Inputs in a Script File in MATLAB MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical computing. In this article, we will see a function with multiple inputs in a script file in MATLAB. Steps:The steps below can be used to build a function with multiple inputs in a MATLAB script file: L
2 min read
Inline Functions in MATLAB Inline functions are those functions that are defined in one line, also called one-liner for the same reason. In MATLAB there are two types of inline functions, inbuilt and user-defined. Let's take a look at both of them. InBuilt inline Functions:MATLAB has many mathematical functions built-in which
2 min read
Scripts and Functions in MATLAB In MATLAB there are a different kinds of files dedicated to MATLAB codes. They are the following: ScriptLive ScriptFunction only fileClass fileNow only the live script is the only one of these which has a different extension name; all other three use the standard .m extension. In this article, we sh
2 min read
Creating Function in Files in MATLAB MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. Functions:The function is a set of statements or commands, which take input/s as parameters and return output. We write functions to
2 min read
Nested Functions in MATLAB Functions in any programming language are some blocks of code, which could be reused whenever required, by just calling the name. It reduces so much of human effort and also rewriting of the same code, and makes the entire code big. Declaring a Function: To declare a function in MATLAB we use given
2 min read
Function Handles in MATLAB Function Handles are a data type of MATLAB which represents a function. They store a function just like an ordinary variable store numeral or alphabetic data. An example of the same could be a function, say f1, that takes another function, f2, as its parameter; f2 calculates a mathematical function
2 min read
How to create a function in MATLAB ? A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: fun
2 min read
Add Functions to Scripts in MATLAB From MATLAB version 2016b, it is possible to add functions directly into a script or live script. In this article, we shall how to add functions to script files. Â The syntax is simple except one rule that the function body must be written after the codes in the script. statement 1 statement 2 . stat
2 min read
Find Index of Cells Containing My String in MATLAB Cell Arrays in MATLAB are a type of array that store data in the form of cells. The benefit of using these cell arrays is that they can store data of different types as cells within a cell array only. In this article, we will see how to find a given string in a cell array. Firstly, we shall check wh
2 min read