Illustration of reading from files, MATLAB in Statistics

Assignment Help:

Illustration of reading from files:

For illustration, suppose that there is a data file 'subjexp.dat' that has on each line a number followed by thecharacter code. The type function can be used to show the contents of this file (as the file does not have the default extension .m, the extension on the filename should be involved).

>> type subjexp.dat

5.3 a

2.2 b

3.3 a

4.4 a

1.1 b

 

The load function would not be capable to read this into the matrix as it holds both numbers and text. Rather than, the fgetl function can be used to read each line as a string and then the string functions are used to break up the numbers and characters. For illustration, the below just reads each line and prints the number with 2 decimal positions and then the rest of the string:

1648_Illustration of reading from files.png

2340_Illustration of reading from files1.png

Here is an illustration of executing this script:

>> fileex

5.30 a

2.20 b

3.30 a

4.40 a

1.10 b

File close successful

In this illustration, in the loop each and every time the fgetl function reads one line into the string variable. The string function strtok is then used to store the number and character in individual variables, both of which are the string variables. The function str2num should then be used to convert the number stored in the string variable into a double variable so that computations could be performed on it.

Rather than of using the fgetl function to read one line, a file has been once opened the fscanf function could be used to read in from this file directly into a matrix.

Though, the matrix should be manipulated somewhat to get it back into the unique form from the file. The format of using the function is as shown below:

mat = fscanf(fid, 'format', [dimensions])


Related Discussions:- Illustration of reading from files

Illustration of nested loops, Illustration of Nested loops: Running th...

Illustration of Nested loops: Running the script shows the output: >> printstars ***** ***** ***** The variable rows identifies the number of rows to print, and

Create a custom chi-square function in matlab, Introduction and Purpose ...

Introduction and Purpose In the lectures and tutorials you have been introduced to a number of probability distributions. You have also learned that modelling any system (such

Graphics objects, Graphics objects: The objects involve graphics primit...

Graphics objects: The objects involve graphics primitives like lines and text, and also the axes used to orient the objects. These objects are organized hierarchically, and the

Example of mode, Example of Mode When no value appears more often than ...

Example of Mode When no value appears more often than any other, the smallest value in the vector will be the mode of the vector. >> shortx = [2 5 1 4]; >> mode(shortx

Nested structures, Nested Structures: The nested structure is a struct...

Nested Structures: The nested structure is a structure in which at least one of the members is itself a structure. For illustration, a structure for the line segment may co

Steps for input output functions - lower level file, Steps for input output...

Steps for input output functions - Lower level file: The steps involved are as shown below:  Open the file.  Read the file, write to the file, or append to the file.

Editor and debugger, Editor/Debugger: The MATLAB has numerous useful f...

Editor/Debugger: The MATLAB has numerous useful functions for debugging, and debugging can also be completed through its editor, which is known as the Editor/Debugger. Typi

Functions which return values versus printing, Functions which return Value...

Functions which return Values versus printing: Also, the function which computes and returns values (throughout the output arguments) does not generally print them; that is le

Index vector, Index vector: The index vector is then used as the indic...

Index vector: The index vector is then used as the indices for original vector. To get grades vector in ascending order, the indices used would be grades (2), grades (5), and

Menu function, MENU Function: The MATLAB also has a built-in function ...

MENU Function: The MATLAB also has a built-in function known as menu which will display a figure Window with push buttons for the choices. The very first string passed to the

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