0% found this document useful (0 votes)
2 views16 pages

Data Structures, Input-Output Operations

The document provides an overview of data structures in programming, specifically focusing on cell arrays, structures, and tables, along with their features and usage in MATLAB. It explains how to create, access, and modify these data structures, as well as input and output operations for user interaction. Additionally, it includes practical exercises for creating a student database and calculating bending stress in a beam using MATLAB.

Uploaded by

htanlik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

Data Structures, Input-Output Operations

The document provides an overview of data structures in programming, specifically focusing on cell arrays, structures, and tables, along with their features and usage in MATLAB. It explains how to create, access, and modify these data structures, as well as input and output operations for user interaction. Additionally, it includes practical exercises for creating a student database and calculating bending stress in a beam using MATLAB.

Uploaded by

htanlik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

COMP113 Computer Technology and Programming

Data Structures, Input-Output Operations

Dr. Abdullah Kendibilir


Faculty of Engineering
Piri Reis University
akendibilir@[Link]
Data Structures
• Data structures are variables that store more than one
value.
Feature Cell Array Structure
• To store more than one value in a variable, the values should
somehow be logically related. Indexed
Named fields
Organization numerically
([Link])
(C{1}, C{2})
• There are many different kinds of data structures.
Collections of
• We have already been working with one kind, arrays (e.g., Best for
unrelated or Grouping related
mixed-type data with names
vectors and matrices). data

• An array is a data structure in which all of the values are Access method
{} for content
. for field access
() for cell
logically related in that they are of the same type and
represent, in some sense, "the same thing." Storing mixed- Representing an
type results object like a
Example use
• We use vectors and matrices when we want to be able to loop from student, car, or
through them (or, essentially, have this done for us using experiments patient
vectorized code).
Cell Array
• A cell array is a kind of data structure that stores values of different types. Cell arrays can
be vectors or matrices; the different values are referred to as the elements of the array.

• Unlike regular arrays (which can only store one type of data, such as all numbers or all
characters), cell arrays can store mixed data types. For example, a combination of numbers,
strings, matrices, or even other cell arrays. Note: Curly braces { } are used to access or assign
the contents of cells.

C = {value1, value2, value3, ...};


C = cell(m, n); % creates an empty m-by-n cell array Accessing Data in a Cell Array:

% Creating a cell array with mixed data •Use curly braces {} to access the content of a cell.
student = {‘Ahmet’, 29905, [40, 87]}; •Use parentheses () to access the cell itself.

% Accessing data from cells


name = student{1}; % returns ‘Ahmet’ student{2} returns the content 29905
studentid = student{2}; % returns 29905 student(2) returns a 1x1 cell containing {[29905]}
grades = student{3}; % returns [40 87]
Structures

• A structure (or struct) is a data type that groups related data using field names.
• Each field can hold data of any type or size.
• You can think of a structure as a folder with named compartments.
• You create a structure using dot notation (.) or the struct function.

% Creating a cell array with mixed data


[Link] = ‘Ahmet';
[Link] = 21; or student = struct('name’,’Ahmet','age’,21,'scores’,[75 85 92]);
[Link] = [75 85 92];

% Accessing data in a structure


[Link]
[Link](2)
Creating Cell Arrays, Referring to and Displaying Cell Array
Elements

Using curly braces for the subscripts will reference the contents of a cell; this is called content indexing.

Using parentheses for the subscripts references the cells; this is called cell indexing.
Creating and Modifying Structure Variables
• Creating structure variables can be accomplished by simply storing values in fields using assignment statements or by
using the struct function.

• The name of the structure variable is package; it has four fields: item_no, cost, price, and code.
• Two way of creating structure variable:

1 2
• To print from a structure, the disp function will display either the entire structure or an individual field.
Practice I

Create 5x4 cell array and structure to create a database which stores the information of
5 students. In this database, the students’ names - students ids - 3 exam scores
and final weighted scores should be included.

Apply the following rules:

1. You can make up your own student names and student id (use randi to give student
id number between 1000-2000).

2. The exam grades can range from 30 to 100, and you should use the randi function to
determine their grades.

3. When calculating the final grade, find the weighted average by taking 25% of the first
and second exam grades and 50% of the final exam.
Tables

• A table is a data structure that stores information in a table format with rows and columns, each of which can be
mnemonically labeled. For example, the following uses the table function to store some simple information for a
doctor's patients.

• Using curly braces to index, the data can be extracted; in the following example, into a double matrix.
Table – Cell – Structure

Feature table cell struct

Data Type Flexibility Medium High Medium

Organization Tabular (columns) Indexed container Named fields

Best Use Case Data analysis Arbitrary data Object-like data

Access Method [Link] / T{1,2} C{1} [Link]


Input and Output
• Input Function: Input statements read in values from the default or standard input
device.

• The input statement reads in values that have been entered by the user, or the
person who is running the script.

• To let the user know what he or she is supposed to enter, the script must first
prompt the user for the specified values. The simplest input function in
MATLAB is called input. The input function is used in an assignment
statement.

• If character or string input is desired, 's' must be added as a second argument to


the input function:

• If the user enters only spaces or tabs before hitting the Enter key, they are
ignored and an empty string is stored in the variable:

• If blank spaces are entered before other characters, they are included in the
string. The length function returns the number of characters in the string.
Input Function
• It is also possible to type quotation marks around the string rather
than including the second argument 's' in the input function. However,
it is better to signify that character input is desired.

• What happens if string input has not been specified, but the user
enters a letter rather than a number.

• MATLAB gave an error message and repeated the prompt. However, if


t is the name of a variable, MATLAB will take its value as the input.

• Separate input statements are necessary if more than one input is


desired. Normally, input statements are suppressed with a
semicolon at the end of the assignment statements.

• It is also possible to enter a vector. The user can enter any valid
vector, using any valid syntax such as square brackets, the colon
operator, or functions such as linspace.
Output Statements: disp and fprint
• Output statements display strings and/or the results of expressions, and can allow for formatting or customizing how they
are displayed.

• The simplest output function in MATLAB is disp, which is used to display the result of an expression or a string without
assigning any value to the default variable ans.

• However, disp does not allow formatting. Formatted output can be printed to the screen using the fprintf
function. For example,

• To the fprintf function, first a string (called the format string) is passed that contains any text to be printed, as well as
formatting information for the expressions to be printed. In this case, the %d is an example of format information.

• The %d is sometimes called a place holder because it specifies where the value of the expression that is after the
string, is to be printed. The character in the place holder is called the conversion character, and it specifies the type
of value that is being printed. A list of the simple place holders:
Output Statements: disp and fprint
• The character '\n' at the end of the string is a special character called the newline character. Output follows moves down
to the next line. Newline character can also be used in the input statement. However, the newline is the ONLY formatting
character allowed in the prompt in input.

• To print two values, there would be two place holders in the format string, and two expressions after the format string.
The expressions fill in for the place holders in sequence.

• For floats, the number of decimal places can also be specified; for example, %6.2f means a field width of 6 (including the
decimal point and the two decimal places) with 2 decimal places. For floats, just the number of decimal places can be
specified; for example, %.3f indicates 3 decimal places, regardless of the field width.
Printing Vectors and Matrices
• For a vector, if a conversion character and the newline character are in the format
string, it will print in a column regardless of whether the vector itself is a row vector
or a column vector.

• Without the newline character, it would print in a row but the next prompt would
appear on the same line

• If the number of elements in the vector is known, that many conversion


characters can be specified:

• For matrices, MATLAB unwinds the matrix column-by-column. For example,


consider the following 2 x 3 matrix:

• Specifying one conversion character and then the newline character will print the
elements from the matrix in one column. The first values printed are from the first
column, then the second column, and so on.
Printing Vectors and Matrices
• If three of the %d conversion characters are specified, the fprintf will
print three numbers across on each line of output, but again the matrix is
unwound column-by-column. It again prints first the two numbers from the
first column (across on the first row of output), then the first value from the
second column, and so on.

• If the transpose of the matrix is printed, however, using the three %d


conversion characters, the matrix is printed as it appears when created.

• For vectors and matrices, even though formatting cannot be specified, the
disp function may be easier to use in general than fprintf because it displays
the result in a straight-forward manner. For example:

• Note that when loops are covered in Chapter 5, formatting the output of matrices
will be easier. For now, however, disp works well.
Practice II
A simply supported beam is subjected to a concentrated load F applied at the midspan. You are asked to calculate
the maximum bending stress in the beam. Given parameters:

•Beam length: L (m)


•Load: F (N) Cross section
h
•Rectangular cross-section dimensions: width b, height h (m)
b
Task:
Write a MATLAB program that:

1. Asks the user to enter the beam parameters,


Formulas to calculate bending stress
2. Computes the maximum bending stress,
3. Displays the results clearly with proper units.

You might also like