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

Example of Functions which complete a task: For illustration, the function below just prints the number arguments passed to it in a sentence format: As this function i

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

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 fu

Working of Editor and debugger: Editor/Debugger, or commands can be typed from Command Window. For illustration, the dbstop command below will set a breakpoint in the fifth li

Run-time or execution-time error: The Run-time, or execution-time, errors are found whenever a script or function is executing. With most of the languages, an illustration of

Data structure layers: There are many layers in this variable. For illustration,  cyls is the whole data structure, that is a vector of structs  cyls(1) is a separate

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

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

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