Read 2*n Data and Write 3*n Data Matlab
MATLAB - Data Import
Importing data in MATLAB ways loading data from an external file. The importdata function allows loading diverse information files of unlike formats. It has the following five forms −
Sr.No. | Part & Description |
---|---|
one | A = importdata(filename) Loads information into assortment A from the file denoted by filename. |
2 | A = importdata('-pastespecial') Loads data from the system clipboard rather than from a file. |
3 | A = importdata(___, delimiterIn) Interprets delimiterIn equally the column separator in ASCII file, filename, or the clipboard information. Yous can use delimiterIn with any of the input arguments in the in a higher place syntaxes. |
iv | A = importdata(___, delimiterIn, headerlinesIn) Loads data from ASCII file, filename, or the clipboard, reading numeric information starting from line headerlinesIn+1. |
5 | [A, delimiterOut, headerlinesOut] = importdata(___) Returns the detected delimiter graphic symbol for the input ASCII file in delimiterOut and the detected number of header lines in headerlinesOut, using any of the input arguments in the previous syntaxes. |
Past default, Octave does not take support for importdata() part, so you will take to search and install this package to make post-obit examples work with your Octave installation.
Case 1
Let u.s. load and display an image file. Create a script file and blazon the following code in it −
filename = 'grin.jpg'; A = importdata(filename); paradigm(A);
When yous run the file, MATLAB displays the image file. Still, you must store it in the current directory.
Example 2
In this example, nosotros import a text file and specify Delimiter and Column Header. Allow us create a infinite-delimited ASCII file with cavalcade headers, named weeklydata.txt.
Our text file weeklydata.txt looks like this −
SunDay MonDay TuesDay Wed ThursDay Friday SaturDay 95.01 76.21 61.54 twoscore.57 55.79 70.28 81.53 73.11 45.65 79.xix 93.55 75.29 69.87 74.68 60.68 41.85 92.18 91.69 81.32 90.38 74.51 48.sixty 82.14 73.82 41.03 0.99 67.22 93.18 89.13 44.47 57.63 89.36 13.89 19.88 46.60
Create a script file and type the post-obit code in it −
filename = 'weeklydata.txt'; delimiterIn = ' '; headerlinesIn = 1; A = importdata(filename,delimiterIn,headerlinesIn); % View data for m = [1:7] disp(A.colheaders{one, k}) disp(A.data(:, m)) disp(' ') end
When yous run the file, it displays the following result −
SunDay 95.0100 73.1100 lx.6800 48.6000 89.1300 Monday 76.2100 45.6500 41.8500 82.1400 44.4700 TuesDay 61.5400 79.1900 92.1800 73.8200 57.6300 Midweek 40.5700 93.5500 91.6900 41.0300 89.3600 Thursday 55.7900 75.2900 81.3200 0.9900 xiii.8900 FriDay seventy.2800 69.8700 90.3800 67.2200 xix.8800 Sat 81.5300 74.6800 74.5100 93.1800 46.6000
Instance iii
In this example, let us import data from clipboard.
Copy the post-obit lines to the clipboard −
Mathematics is simple
Create a script file and type the following lawmaking −
A = importdata('-pastespecial')
When you lot run the file, it displays the following result −
A = 'Mathematics is simple'
Depression-Level File I/O
The importdata function is a loftier-level function. The low-level file I/O functions in MATLAB allow the most command over reading or writing data to a file. Even so, these functions need more detailed information about your file to work efficiently.
MATLAB provides the following functions for read and write operations at the byte or character level −
Function | Description |
---|---|
fclose | Shut i or all open files |
feof | Test for end-of-file |
ferror | Information nigh file I/O errors |
fgetl | Read line from file, removing newline characters |
fgets | Read line from file, keeping newline characters |
fopen | Open file, or obtain information about open up files |
fprintf | Write information to text file |
fread | Read data from binary file |
frewind | Move file position indicator to beginning of open file |
fscanf | Read data from text file |
fseek | Motion to specified position in file |
ftell | Position in open file |
fwrite | Write data to binary file |
Import Text Information Files with Low-Level I/O
MATLAB provides the post-obit functions for low-level import of text data files −
-
The fscanf office reads formatted data in a text or ASCII file.
-
The fgetl and fgets functions read ane line of a file at a time, where a newline character separates each line.
-
The fread role reads a stream of information at the byte or bit level.
Example
We take a text data file 'myfile.txt' saved in our working directory. The file stores rainfall data for three months; June, July and August for the year 2012.
The data in myfile.txt contains repeated sets of time, month and rainfall measurements at five places. The header data stores the number of months M; and so nosotros accept M sets of measurements.
The file looks like this −
Rainfall Data Months: June, July, Baronial M = 3 12:00:00 June-2012 17.21 28.52 39.78 16.55 23.67 nineteen.15 0.35 17.57 NaN 12.01 17.92 28.49 17.twoscore 17.06 11.09 9.59 ix.33 NaN 0.31 0.23 x.46 13.17 NaN xiv.89 19.33 20.97 19.l 17.65 14.45 xiv.00 18.23 x.34 17.95 xvi.46 nineteen.34 09:10:02 July-2012 12.76 sixteen.94 14.38 xi.86 xvi.89 xx.46 23.17 NaN 24.89 19.33 thirty.97 49.l 47.65 24.45 34.00 18.23 30.34 27.95 16.46 19.34 30.46 33.17 NaN 34.89 29.33 30.97 49.l 47.65 24.45 34.00 28.67 30.34 27.95 36.46 29.34 15:03:40 August-2012 17.09 xvi.55 xix.59 17.25 19.22 17.54 xi.45 13.48 22.55 24.01 NaN 21.19 25.85 25.05 27.21 26.79 24.98 12.23 16.99 eighteen.67 17.54 11.45 13.48 22.55 24.01 NaN 21.19 25.85 25.05 27.21 26.79 24.98 12.23 16.99 18.67
Nosotros volition import information from this file and display this information. Have the post-obit steps −
-
Open up the file with fopen function and become the file identifier.
-
Depict the data in the file with format specifiers, such as '%s' for a string, '%d' for an integer, or '%f' for a floating-point number.
-
To skip literal characters in the file, include them in the format description. To skip a information field, use an asterisk ('*') in the specifier.
For example, to read the headers and return the single value for M, we write −
Thousand = fscanf(fid, '%*s %*s\n%*south %*s %*due south %*s\nM=%d\northward\n', 1);
-
By default, fscanf reads information co-ordinate to our format description until it does not notice any match for the data, or it reaches the end of the file. Hither we will utilize for loop for reading iii sets of information and each time, it will read 7 rows and 5 columns.
-
We will create a structure named mydata in the workspace to store data read from the file. This structure has three fields - time, month, and raindata array.
Create a script file and type the following code in it −
filename = '/data/myfile.txt'; rows = vii; cols = 5; % open up the file fid = fopen(filename); % read the file headers, find M (number of months) M = fscanf(fid, '%*southward %*s\n%*due south %*due south %*due south %*s\nM=%d\n\n', 1); % read each set of measurements for northward = i:One thousand mydata(north).time = fscanf(fid, '%due south', 1); mydata(due north).calendar month = fscanf(fid, '%southward', ane); % fscanf fills the array in column order, % so transpose the results mydata(north).raindata = ... fscanf(fid, '%f', [rows, cols]); end for n = 1:Yard disp(mydata(n).fourth dimension), disp(mydata(n).month) disp(mydata(north).raindata) end % close the file fclose(fid);
When you lot run the file, it displays the post-obit event −
12:00:00 June-2012 17.2100 17.5700 11.0900 thirteen.1700 fourteen.4500 28.5200 NaN 9.5900 NaN fourteen.0000 39.7800 12.0100 9.3300 14.8900 18.2300 sixteen.5500 17.9200 NaN 19.3300 ten.3400 23.6700 28.4900 0.3100 20.9700 17.9500 xix.1500 17.4000 0.2300 19.5000 16.4600 0.3500 17.0600 ten.4600 17.6500 19.3400 09:10:02 July-2012 12.7600 NaN 34.0000 33.1700 24.4500 sixteen.9400 24.8900 18.2300 NaN 34.0000 14.3800 19.3300 xxx.3400 34.8900 28.6700 eleven.8600 thirty.9700 27.9500 29.3300 thirty.3400 sixteen.8900 49.5000 16.4600 30.9700 27.9500 20.4600 47.6500 nineteen.3400 49.5000 36.4600 23.1700 24.4500 30.4600 47.6500 29.3400 15:03:40 Baronial-2012 17.0900 thirteen.4800 27.2100 11.4500 25.0500 16.5500 22.5500 26.7900 thirteen.4800 27.2100 nineteen.5900 24.0100 24.9800 22.5500 26.7900 17.2500 NaN 12.2300 24.0100 24.9800 19.2200 21.1900 sixteen.9900 NaN 12.2300 17.5400 25.8500 eighteen.6700 21.1900 16.9900 11.4500 25.0500 17.5400 25.8500 18.6700
Useful Video Courses
Video
Video
Video
Video
Video
Video
Source: https://www.tutorialspoint.com/matlab/matlab_data_import.htm
0 Response to "Read 2*n Data and Write 3*n Data Matlab"
Post a Comment