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

Need of a nested loop, Need of a nested loop: How would we sum each i...

Need of a nested loop: How would we sum each individual column, rather than getting an overall sum? Answer: The programming technique would need a nested loop in whi

Curve fitting, Curve Fitting: The data is fundamentally either discret...

Curve Fitting: The data is fundamentally either discrete or continuous. In most of the applications, data is sampled, for illustration, The temperature recorded every ho

Nested for loops, Nested for Loops: The action of a loop can be any of...

Nested for Loops: The action of a loop can be any of the valid statement(s). Whenever the action of a loop is the other loop, this is known as nested loop. As an illustrati

Properties of the text box - graphics objects, Properties of the text box -...

Properties of the text box - graphics objects: By using get will show properties of the text box, the illustration is as shown below: >> get(thand)   BackgroundColor

Mode, Mode The mode of a data set is the value which appears most often...

Mode The mode of a data set is the value which appears most often. The built-in function in a MATLAB for this is known as the mode. >> x = [9 10 10 9 8 7 3 10 9 8 5 10];

Example of functions which return values, Example of Functions which return...

Example of Functions which return values: Calling this function prints the circumference, since there is no way to store the value, hence, it can be used in following computat

Program of persistent variables, Program of Persistent variables: The ...

Program of Persistent variables: The program below elaborates this. The script calls a function func1 that initializes a variable count to 0, then increment it, and print the

Core objects, Core Objects: The Core Objects in MATLAB are the very fu...

Core Objects: The Core Objects in MATLAB are the very fundamental graphics primitives. The description can be found under the MATLAB Help: Under the Contents tab, click the Ha

Illustrations of sequential search, Illustrations of Sequential search: ...

Illustrations of Sequential search: The two illustrations of calling such function is as shown below: >> values = [85 70 100 95 80 91]; >> key = 95; >> seqsearch

Combining nested for loops and the if statements, Combining Nested FOR Loop...

Combining Nested FOR Loops and the IF Statements: The statements inside a nested loop can be any of the valid statement, involving any selection statement. For e.g., there can

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