Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How to use textscan to show string? for example: Currency Date Price Open High L

ID: 3682679 • Letter: H

Question

How to use textscan to show string?

for example:

Currency   Date   Price   Open   High   Low   Change %

EUR/USD   3/1/2016   1.118   1.0886   1.1217   1.0823   2.82%
EUR/USD   2/1/2016   1.0873   1.0831   1.1377   1.0812   0.33%
EUR/USD   1/1/2016   1.0837   1.086   1.0986   1.0709   -0.21%
EUR/USD   12/1/2015   1.086   1.0566   1.1059   1.0538   2.80%
EUR/USD   11/1/2015   1.0564   1.1013   1.1053   1.0556   -4.01%
EUR/USD   10/1/2015   1.1005   1.1175   1.1496   1.0894   -1.54%

How to show the whole column of currency, date and change% using textscan?

Explanation / Answer

textscan
Read data from text file, convert, and write to cell array

Syntax

Description

Before reading a file with textscan, you must open the file with the fopen function. fopen supplies the fid input required by textscan. When you are finished reading from the file, you should close the file by calling fclose(fid).

C = textscan(fid, 'format') reads data from an open text file identified by file identifier fid into cell array C. MATLAB parses the data into fields and converts it according to the conversion specifiers in the format string. These conversion specifiers determine the type of each cell in the output cell array. The number of specifiers determines the number of cells in the cell array.

C = textscan(fid, 'format', N) reads data from the file, reusing the format conversion specifier N times, where N is a positive integer. You can resume reading from the file after N cycles by calling textscanagain using the original fid.

C = textscan(fid, 'format', param, value, ...) reads data from the file using nondefault parameter settings specified by one or more pairs of param and value arguments. The section User Configurable Options lists all valid parameter strings, value descriptions, and defaults.

C = textscan(fid, 'format', N, param, value, ...) reads data from the file, reusing the format conversion specifier N times, and using nondefault parameter settings specified by pairs of param and valuearguments.

C = textscan(str, ...) reads data from string str in exactly the same way as it does when reading from a file. You can use the format, N, and parameter/value arguments described above with this syntax. Unlike when reading from a file, if you call textscan more than once on the same string, it does not resume reading where the last call left off but instead reads from the beginning of the string each time.

[C, position] = textscan(...) returns the location of the file or string position as the second output argument. For a file, this is exactly equivalent to calling ftell(fid) after making the call to TEXTSCAN. For a string, it indicates how many characters were read.

Field Delimiters

The textscan function regards a text file as consisting of blocks. Each block consists of a number of internally consistent fields. Each field consists of a group of characters delimited by a field delimiter character. Fields can span a number of rows. Each row is delimited by an end-of-line (EOL) character sequence.

The default field delimiter is the white-space character, You can set the delimiter to a different character by specifying a 'delimiter'parameter in the textscan command. If a non default delimiter is specified, repeated delimiter characters are treated as separate delimiters. When using the default delimiter, repeated white-space characters are treated as a single delimiter.

The default end-of-line character sequence depends on which operating system you are using. You can set end-of-line to a different character sequence by specifying an 'endofline' parameter in the textscan command If you set the delimiter parameter to 'EOL' (using the third syntax shown above), textscan reads complete rows.

Examples

Example 1-- Reading Different Types of Data

Text file scan1.dat contains data in the following form:

CS Level1 12.34 45 1.23e10 inf NaN Yes

EC Level2 23.54 60 9e19 -inf 0.001 No

EE   Level3 34.90 12 2e5 10 100 No

Read each column into a variable:

fid = fopen('scan1.dat');

C = textscan(fid, '%s %s %f32 %d8 %u %f %f %s');

fclose(fid);

textscan returns a 1-by-8 cell array C with the following cells:

C{1} = {'CS'; 'EC'; 'EE'}          class cell

C{2} = {'Level1'; 'Level2'; 'Level3'}    class cell

C{3} = [12.34; 23.54; 34.9]              class single

C{4} = [45; 60; 12]                      class int8

C{5} = [4294967295; 4294967295; 200000] class uint32

C{6} = [Inf; -Inf; 10]                   class double

C{7} = [NaN; 0.001; 100]                 class double

C{8} = {'Yes'; 'No'; 'No'}               class cell

The first two elements of C{5} are the maximum values for a 32-bit unsigned integer, or intmax('uint32').

Example 2 -- Reading All But One Field

Read the file as a fixed-format file, skipping the third field:

fid = fopen('scan1.dat');

C = textscan(fid, '%7c %6s %*f %d8 %u %f %f %s');

fclose(fid);

textscan returns a 1-by-8 cell array C with the following cells:

C{1} = ['CS '; 'EC    '; 'EE   ']   class char

C{2} = {'Level1'; 'Level2'; 'Level3'}      class cell

C{3} = [45; 60; 12]                        class int8

C{4} = [4294967295; 4294967295; 200000]    class uint32

C{5} = [Inf; -Inf; 10]                     class double

C{6} = [NaN; 0.001; 100]                   class double

C{7} = {'Yes'; 'No'; 'No'}                 class cell

Example 3 -- Reading Only the First Field

Read the first column into a cell array, skipping the rest of the line:

fid = fopen('scan1.dat');

names = textscan(fid, '%s%*[^ ]');

fclose(fid);

textscan returns a 1-by-1 cell array names:

size(names)

ans =

     1     1

The one cell contains

names{1} = {'CS'; 'EC'; 'EE'}       class cell

note-the above explanation can help to answer the given question.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote