Advanced file input and output, MATLAB in Statistics

Advanced File Input and output:

Where 'format' involves the conversion characters much like those used in the fprintf function. For illustration, to read the file 'subjexp.dat' just shown, we can do the following (again, for ease, omitting the error-check of fopen & fclose):

>> fid = fopen('subjexp.dat');

>> subjdata = textscan(fid,'%f %c');

>> fclose(fid)

 

The format string '%f %c' identifies that on each line there is a double value followed by the space followed by the character. This generates a 1 × 2 cell array variable known as the subjdata. The initial element in this cell array is a column vector of doubles (the initial column from the file); the next element is a column vector of characters (the next column from the file), as shown here:

 

>> subjdata

subjdata =

    [5x1 double] [5x1 char]

>> subjdata{1}

ans =

    5.3000

    2.2000

    3.3000

    4.4000

    1.1000

>> subjdata{2}

ans =

a

b

a

a

b

To refer to separate values from the vector, it is essential to index into the cell array by using curly braces and then index into the vector by using the parentheses. 

Posted Date: 10/20/2012 6:26:55 AM | Location : United States







Related Discussions:- Advanced file input and output, Assignment Help, Ask Question on Advanced file input and output, Get Answer, Expert's Help, Advanced file input and output Discussions

Write discussion on Advanced file input and output
Your posts are moderated
Related Questions
Debugging Techniques: Any error in the computer program is called a bug. This word is thought to date back to the 1940s, whenever a problem with an early computer was found to

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

Illustration of Input in a for loop: In this illustration, the loop variable iv iterates through the values 1 through 3, therefore the action is repeated three times. The acti

Opening and Closing a File: The Files are opened with the fopen function. By the default, fopen function opens a file for reading. If the other mode is preferred, a permission

Logical Built-In Functions: There are built-in functions in the MATLAB which are useful in conjunction with vectors or matrices of all logical true or false values; two of the

Example of Median For the vector [1 4 5 9 12 33], the median is the average of the 5 & 9 in the middle: >> median([1 4 5 9 12 33]) ans =     7

Example of Nested functions: For illustration, the function below computes and returns the volume of a cube. Three arguments are then passed to it, for the width and length of

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

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