Passing structures to functions, MATLAB in Engineering

Assignment Help:

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.


Related Discussions:- Passing structures to functions

Example of image processing, Example of image processing: The other il...

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

Creating a cell array - assign values to array, Creating a cell array: ...

Creating a cell array: The other method of creating a cell array is easy to assign values to particular array elements and build it up element by element. Though, as explained

Subfunctions, Subfunctions: Though, it is possible to have more than o...

Subfunctions: Though, it is possible to have more than one function in a given M-file. For illustration, if one function calls the other, the first function would be the prima

Changing case, Changing Case: The MATLAB has two functions which conve...

Changing Case: The MATLAB has two functions which convert strings to all uppercase letters, or all lowercase, known as the upper and lower. >> mystring = 'AbCDEfgh';

Function imread - image processing, function imread: The function imre...

function imread: The function imread can read an image file, for illustration a JPEG (.jpg) file. The function reads color images into a 3-dimensional matrix. >> myimage1

Signal and system, . Generate the following signal, x(n)=1+cos((25*pi...

. Generate the following signal, x(n)=1+cos((25*pi*n)/100),0 Compute the DTFT of x[n] for w=0:0.01:2*pi Plot the Real part, imaginary part, the amplitude and phas

Function call - modular program, Function call: In the function call, ...

Function call: In the function call, not any arguments are passed so there are no input arguments in the function header. The function returns an output argument, therefore th

Function issorted - set operations, Function issorted - set operations: ...

Function issorted - set operations: The function issorted will return 1 for logical true when the argument is sorted in ascending order (minimum to maximum), or 0 for false wh

Illustration sorting vectors of structures, Illustration sorting vectors of...

Illustration sorting vectors of structures: This function sorts the structures depend only on the price field. A more common function is shown next, that receives a string whi

Simplification functions, Simplification Functions: There are numerous...

Simplification Functions: There are numerous functions which work with expressions, and simplify the terms. Not all the expressions can be simplified, but the simplify functio

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd