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
Sorting Strings: For the matrix of strings, the sort function works exactly as formerly for numbers. For illustration, >> words = char('Hi', 'Hello', 'Howdy', 'Goodbye', '

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

Lower Level File I/O Functions: Whenever reading from data file, as long as the data in the file is "regular" the load function works-in another words, the similar type of dat

Statistics There are numerous statistical analyses which can be executed on data sets. In MATLAB software, the statistical functions are in the data analysis help topic known a

Illustration of tracing a error: The one way of following the flow of function, or tracing it, is to use the echo function. The echo function, that is a toggle, will show each

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

Execution of persistent variables: The functions can also be called from the script or from the Command Window, as shown here. For illustration, the functions are called first

fscanf function - file function: The fscanf reads matrix variable mat columnwise from the file specified by fid. The 'format' involves conversion characters much similar to th

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

Illustration of nested for loops and the if statements: For illustration,when the file contains: 33   -11  2  4      5   9 22     5  -7  2     11    3 the outco