Passing structures to functions, MATLAB in Engineering

Passing Structures to Functions:

The whole structure can be passed to a function, or separate fields can be passed. For illustration, here are the two distinct versions of a function which computes the profit on a software package. The profit is then defined as the price minus the cost.

In the first version, the whole structure variable is passed to the function, therefore the function should use the dot operator to refer to the price and cost fields of the input argument.

653_Passing Structures to Functions.png

>> calcprof(package)

ans =

19.9600

In the second version, merely the price and cost fields are passed to the function by using the dot operator in the function call. These are passed to the two scalar input arguments in function header; therefore there is no reference to a structure variable in the function itself, and the dot operator is not required in the function.

2117_Passing Structures to Functions1.png

>> calcprof2(package.price, package.cost)

ans =

19.9600

 

It is significant, as always with the functions, to make sure that the arguments in the function call correspond one-to-one with the input arguments in the function header. In case of calcprof, the structure variable is passed to an input argument that is a structure. For second function calcprof2, the two individual fields, that is double values, and is passed to two double arguments.

Posted Date: 10/20/2012 5:59:51 AM | Location : United States







Related Discussions:- Passing structures to functions, Assignment Help, Ask Question on Passing structures to functions, Get Answer, Expert's Help, Passing structures to functions Discussions

Write discussion on Passing structures to functions
Your posts are moderated
Related Questions
Splits a string : The strtok function splits a string into pieces; it can be called in many ways. The function receives one string as an input argument. It appears for the fir

True color matrice: The true color matrices are the other way to represent images. The true color matrices are 3-dimensional matrices. The first two coordinates are the coordi

Illustration of Vectors of structures: In this illustration, the packages are vector which has three elements. It is shown as a column vector. Each and every element is a stru

Example of image processing: The other illustration generates a 5 × 5 matrix of arbitrary integers in the range from 1 to the number of colors; the resultant image is as shown

Illustration of symbolic variable: When, on the other hand, z is a symbolic variable to start with, quotes are not required around the expression, and the words are automatica

Forward substitution: The Forward substitution (done methodically by first getting a 0 in the a 21 place, and then a 31 , and lastly a 32 ): For the Gauss technique,

Illustration of if - else statement: The one application of an if-else statement is to check for errors in the inputs to a script. For illustration, a former script prompted t

Replacing a string - function strrep: The function strrep finds all the occurrences of a substring within the string, and substitutes them with a new substring. The order of a

Illustration of anonymous functions: Dissimilar functions stored in the M-files, when no argument is passed to an anonymous function, the parentheses should still be in the fu

Inverse of square matrix: The inverse is, hence the result of multiplying the scalar 1/D by each and every element in the preceding matrix. Note that this is not the matrix A,