Illustration of reading from files, MATLAB in Statistics

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])

Posted Date: 10/20/2012 6:24:04 AM | Location : United States







Related Discussions:- Illustration of reading from files, Assignment Help, Ask Question on Illustration of reading from files, Get Answer, Expert's Help, Illustration of reading from files Discussions

Write discussion on Illustration of reading from files
Your posts are moderated
Related Questions
Variable packages: Variable packages are now the vector of structures, hence each and every element in the vector is a structure. To show one element in the vector, an index i

Illustration of Binary search: An illustration to search for the key of 91 in the vector is as shown below: The table below shows what will happen in each iteration of

Reading from Files: There are many lower level functions which read from files. The function fscanf reads the formatted data into a matrix, by using conversion formats like %s

Input in a While Loop: The script below repeats the procedure of prompting a user, the reading in a positive number, and echo-printing it, as long as the user properly enters

Example of sorting strings: To sort a cell array of strings, also the sort function is used. The illustration is as follows, >> engcellnames = {'Chemical','Mechanical',.

Help function: Function is computing the two values, there are two output arguments in the function header (i.e., area & circum), that are placed in the square brackets [ ]. A

Illustration of Customizing plots: Illustration, the bar and barh functions by the default place a width of 0.8 between bars. Whenever called as bar(x,y), the width of 0.8 is

Illustration of Median When the vector is not in sorted order to start with, the median function will still return the right answer (it will sort the vector automatically). For

The IF Statement: The if statement selects whether or not the other statement, or group of statements, is executed. The common form of the if statement is as shown below: i

Example of Function stubs: The three functions have not so far been written, though, therefore the function stubs are put in place so that the script can be executed and teste