SlideShare a Scribd company logo
1 of 5
Download to read offline
http://www.tutorialspoint.com/matlab/matlab_data_import.htm Copyright © tutorialspoint.com
MATLAB - DATA IMPORT
Importing data inMATLAB means loading data fromanexternalfile. The importdata functionallows loading
various data files of different formats. It has the following five forms:
S.N. Function and Description
1 A = importdata(filename)
Loads data into array A fromthe file denoted by filename.
2 A = importdata('-pastespecial')
Loads data fromthe systemclipboard rather thanfroma file.
3 A = importdata(___, delimiterIn)
Interprets delimiterIn as the columnseparator inASCII file, filename, or the clipboard data. Youcan
use delimiterIn withany of the input arguments inthe above syntaxes.
4 A = importdata(___, delimiterIn, headerlinesIn)
Loads data fromASCII file, filename, or the clipboard, reading numeric data starting fromline
headerlinesIn+1.
5 [A, delimiterOut, headerlinesOut] = importdata(___)
dditionally returns the detected delimiter character for the input ASCII file indelimiterOut and the
detected number of header lines inheaderlinesOut, using any of the input arguments inthe previous
syntaxes.
By default, Octave does not have support for importdata() function, so you will have to search
and install this package to make following examples work with your Octave installation.
Example 1
Let us load and display animage file. Create a script file and type the following code init:
filename = 'smile.jpg';
A = importdata(filename);
image(A);
Whenyourunthe file, MATLAB displays the image file. However, youmust store it inthe current directory.
Example 2
Inthis example, we import a text file and specify Delimiter and ColumnHeader. Let us create a space-delimited
ASCII file withcolumnheaders, named weeklydata.txt.
Our text file weeklydata.txt looks like this:
SunDay MonDay TuesDay WednesDay ThursDay FriDay SatureDay
95.01 76.21 61.54 40.57 55.79 70.28 81.53
73.11 45.65 79.19 93.55 75.29 69.87 74.68
60.68 41.85 92.18 91.69 81.32 90.38 74.51
48.60 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 following code init:
filename = 'weeklydata.txt';
delimiterIn = ' ';
headerlinesIn = 1;
A = importdata(filename,delimiterIn,headerlinesIn);
% View data
for k = [1:7]
disp(A.colheaders{1, k})
disp(A.data(:, k))
disp(' ')
end
Whenyourunthe file, it displays the following result:
SunDay
95.0100
73.1100
60.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
WednesDay
40.5700
93.5500
91.6900
41.0300
89.3600
ThursDay
55.7900
75.2900
81.3200
0.9900
13.8900
FriDay
70.2800
69.8700
90.3800
67.2200
19.8800
SatureDay
81.5300
74.6800
74.5100
93.1800
46.6000
Example 3
Inthis example, let us import data fromclipboard.
Copy the following lines to the clipboard:
Mathematics is simple
Create a script file and type the following code:
A = importdata('-pastespecial')
Whenyourunthe file, it displays the following result:
A =
'Mathematics is simple'
Low-Level File I/O
The importdata functionis a high-levelfunction. The low-levelfile I/O functions inMATLAB allow the most
controlover reading or writing data to a file. However, these functions need more detailed informationabout
your file to work efficiently.
MATLAB provides the following functions for read and write operations at the byte or character level:
Function Description
fclose Close one or allopenfiles
feof Test for end-of-file
ferror Informationabout file I/O errors
fgetl Read line fromfile, removing newline characters
fgets Read line fromfile, keeping newline characters
fopen Openfile, or obtaininformationabout openfiles
fprintf Write data to text file
fread Read data frombinary file
frewind Move file positionindicator to beginning of openfile
fscanf Read data fromtext file
fseek Move to specified positioninfile
ftell Positioninopenfile
fwrite Write data to binary file
Import Text Data Files with Low-Level I/O
MATLAB provides the following functions for low-levelimport of text data files:
The fscanf functionreads formatted data ina text or ASCII file.
The fgetl and fgets functions read one line of a file at a time, where a newline character separates each
line.
The fread functionreads a streamof data at the byte or bit level.
Example
We have a text data file 'myfile.txt' saved inour working directory. The file stores rainfalldata for three months;
June, July and August for the year 2012.
The data inmyfile.txt contains repeated sets of time, monthand rainfallmeasurements at five places. The header
data stores the number of months M; so we have M sets of measurements.
The file looks like this:
Rainfall Data
Months: June, July, August
M=3
12:00:00
June-2012
17.21 28.52 39.78 16.55 23.67
19.15 0.35 17.57 NaN 12.01
17.92 28.49 17.40 17.06 11.09
9.59 9.33 NaN 0.31 0.23
10.46 13.17 NaN 14.89 19.33
20.97 19.50 17.65 14.45 14.00
18.23 10.34 17.95 16.46 19.34
09:10:02
July-2012
12.76 16.94 14.38 11.86 16.89
20.46 23.17 NaN 24.89 19.33
30.97 49.50 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.50 47.65 24.45 34.00
28.67 30.34 27.95 36.46 29.34
15:03:40
August-2012
17.09 16.55 19.59 17.25 19.22
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
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
We willimport data fromthis file and display this data. Take the following steps:
1. Openthe file withfopen functionand get the file identifier.
2. Describe the data inthe file withformat specifiers, suchas '%s' for a string, '%d' for aninteger, or
'%f' for a floating-point number.
3. To skip literalcharacters inthe file, include theminthe format description. To skip a data field, use an
asterisk ('*') inthe specifier.
For example, to read the headers and returnthe single value for M, we write:
M = fscanf(fid, '%*s %*sn%*s %*s %*s %*snM=%dnn', 1);
4. By default, fscanf reads data according to our format descriptionuntilit cannot matchthe descriptionto
the data, or it reaches the end of the file. Here we willuse for loop for reading 3 sets of data and eachtime,
it willread 7 rows and 5 columns.
5. We willcreate a structure named mydata inthe workspace to store data read fromthe file. This structure
has three fields - time, month, and raindata array.
Create a script file and type the following code init:
filename = '/data/myfile.txt';
rows = 7;
cols = 5;
% open the file
fid = fopen(filename);
% read the file headers, find M (number of months)
M = fscanf(fid, '%*s %*sn%*s %*s %*s %*snM=%dnn', 1);
% read each set of measurements
for n = 1:M
mydata(n).time = fscanf(fid, '%s', 1);
mydata(n).month = fscanf(fid, '%s', 1);
% fscanf fills the array in column order,
% so transpose the results
mydata(n).raindata = ...
fscanf(fid, '%f', [rows, cols]);
end
for n = 1:M
disp(mydata(n).time), disp(mydata(n).month)
disp(mydata(n).raindata)
end
% close the file
fclose(fid);
Whenyourunthe file, it displays the following result:
12:00:00
June-2012
17.2100 17.5700 11.0900 13.1700 14.4500
28.5200 NaN 9.5900 NaN 14.0000
39.7800 12.0100 9.3300 14.8900 18.2300
16.5500 17.9200 NaN 19.3300 10.3400
23.6700 28.4900 0.3100 20.9700 17.9500
19.1500 17.4000 0.2300 19.5000 16.4600
0.3500 17.0600 10.4600 17.6500 19.3400
09:10:02
July-2012
12.7600 NaN 34.0000 33.1700 24.4500
16.9400 24.8900 18.2300 NaN 34.0000
14.3800 19.3300 30.3400 34.8900 28.6700
11.8600 30.9700 27.9500 29.3300 30.3400
16.8900 49.5000 16.4600 30.9700 27.9500
20.4600 47.6500 19.3400 49.5000 36.4600
23.1700 24.4500 30.4600 47.6500 29.3400
15:03:40
August-2012
17.0900 13.4800 27.2100 11.4500 25.0500
16.5500 22.5500 26.7900 13.4800 27.2100
19.5900 24.0100 24.9800 22.5500 26.7900
17.2500 NaN 12.2300 24.0100 24.9800
19.2200 21.1900 16.9900 NaN 12.2300
17.5400 25.8500 18.6700 21.1900 16.9900
11.4500 25.0500 17.5400 25.8500 18.6700

More Related Content

What's hot

pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data AnalysisAndrew Henshaw
 
Manipulating data with dates
Manipulating data with datesManipulating data with dates
Manipulating data with datesRupak Roy
 
Generator analysis
Generator analysisGenerator analysis
Generator analysisVicSteadman
 
N_Asm Assembly arrays (sol)
N_Asm Assembly arrays (sol)N_Asm Assembly arrays (sol)
N_Asm Assembly arrays (sol)Selomon birhane
 
MySQL Manchester TT - JSON Example
MySQL Manchester TT - JSON ExampleMySQL Manchester TT - JSON Example
MySQL Manchester TT - JSON ExampleMark Swarbrick
 
Chunked, dplyr for large text files
Chunked, dplyr for large text filesChunked, dplyr for large text files
Chunked, dplyr for large text filesEdwin de Jonge
 
Data mining for the masses, Chapter 4, Using R instead of Rapidminer
Data mining for the masses, Chapter 4, Using R instead of RapidminerData mining for the masses, Chapter 4, Using R instead of Rapidminer
Data mining for the masses, Chapter 4, Using R instead of RapidminerUlrik Hørlyk Hjort
 
Cs 151 dictionary writer
Cs 151 dictionary writerCs 151 dictionary writer
Cs 151 dictionary writerRudy Martinez
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In RRsquared Academy
 
Distributing C# Applications with Apache Spark (TechEd 2017, Prague)
Distributing C# Applications with Apache Spark (TechEd 2017, Prague)Distributing C# Applications with Apache Spark (TechEd 2017, Prague)
Distributing C# Applications with Apache Spark (TechEd 2017, Prague)Attila Szucs
 
Data mining for the masses, Chapter 6, Using R as an alternative to Rapidminer
Data mining for the masses, Chapter 6, Using R as an alternative to RapidminerData mining for the masses, Chapter 6, Using R as an alternative to Rapidminer
Data mining for the masses, Chapter 6, Using R as an alternative to RapidminerUlrik Hørlyk Hjort
 

What's hot (18)

pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data Analysis
 
R Get Started II
R Get Started IIR Get Started II
R Get Started II
 
Manipulating data with dates
Manipulating data with datesManipulating data with dates
Manipulating data with dates
 
Generator analysis
Generator analysisGenerator analysis
Generator analysis
 
N_Asm Assembly arrays (sol)
N_Asm Assembly arrays (sol)N_Asm Assembly arrays (sol)
N_Asm Assembly arrays (sol)
 
Getting started with R
Getting started with RGetting started with R
Getting started with R
 
MySQL Manchester TT - JSON Example
MySQL Manchester TT - JSON ExampleMySQL Manchester TT - JSON Example
MySQL Manchester TT - JSON Example
 
Chunked, dplyr for large text files
Chunked, dplyr for large text filesChunked, dplyr for large text files
Chunked, dplyr for large text files
 
Raster package jacob
Raster package jacobRaster package jacob
Raster package jacob
 
Data mining for the masses, Chapter 4, Using R instead of Rapidminer
Data mining for the masses, Chapter 4, Using R instead of RapidminerData mining for the masses, Chapter 4, Using R instead of Rapidminer
Data mining for the masses, Chapter 4, Using R instead of Rapidminer
 
Arrays in SAS
Arrays in SASArrays in SAS
Arrays in SAS
 
Cs 151 dictionary writer
Cs 151 dictionary writerCs 151 dictionary writer
Cs 151 dictionary writer
 
R Programming: Importing Data In R
R Programming: Importing Data In RR Programming: Importing Data In R
R Programming: Importing Data In R
 
Distributing C# Applications with Apache Spark (TechEd 2017, Prague)
Distributing C# Applications with Apache Spark (TechEd 2017, Prague)Distributing C# Applications with Apache Spark (TechEd 2017, Prague)
Distributing C# Applications with Apache Spark (TechEd 2017, Prague)
 
Data mining for the masses, Chapter 6, Using R as an alternative to Rapidminer
Data mining for the masses, Chapter 6, Using R as an alternative to RapidminerData mining for the masses, Chapter 6, Using R as an alternative to Rapidminer
Data mining for the masses, Chapter 6, Using R as an alternative to Rapidminer
 
Coding pilkades
Coding pilkadesCoding pilkades
Coding pilkades
 
R factors
R   factorsR   factors
R factors
 
Queue
QueueQueue
Queue
 

Viewers also liked

Data export in matlab alvian zainuddin
Data export in matlab alvian zainuddinData export in matlab alvian zainuddin
Data export in matlab alvian zainuddinAlvianzainuddin
 
Experiment6matlab
Experiment6matlabExperiment6matlab
Experiment6matlabAshok Singh
 
Import/Export data in Matlab
Import/Export data in MatlabImport/Export data in Matlab
Import/Export data in Matlabchingtony mbuma
 
Basics of Image Processing using MATLAB
Basics of Image Processing using MATLABBasics of Image Processing using MATLAB
Basics of Image Processing using MATLABE2MATRIX
 
Intro matlab and convolution islam
Intro matlab and convolution islamIntro matlab and convolution islam
Intro matlab and convolution islamIslam Alabbasy
 
MATLAB CODE OF FIR Filter Designing LPF HPF BPF BSF
MATLAB CODE OF FIR Filter Designing LPF HPF BPF BSFMATLAB CODE OF FIR Filter Designing LPF HPF BPF BSF
MATLAB CODE OF FIR Filter Designing LPF HPF BPF BSFBharti Airtel Ltd.
 
Brief Introduction to Matlab
Brief  Introduction to MatlabBrief  Introduction to Matlab
Brief Introduction to MatlabTariq kanher
 
Introduction to simulink (1)
Introduction to simulink (1)Introduction to simulink (1)
Introduction to simulink (1)Memo Love
 
Matlab and Image Processing Workshop-SKERG
Matlab and Image Processing Workshop-SKERG Matlab and Image Processing Workshop-SKERG
Matlab and Image Processing Workshop-SKERG Sulaf Almagooshi
 
Digital image processing using matlab (fundamentals)
Digital image processing using matlab (fundamentals)Digital image processing using matlab (fundamentals)
Digital image processing using matlab (fundamentals)Taimur Adil
 
[Steven karris] introduction_to_simulink_with_engi
[Steven karris] introduction_to_simulink_with_engi[Steven karris] introduction_to_simulink_with_engi
[Steven karris] introduction_to_simulink_with_engiStiedy Jocky
 

Viewers also liked (20)

Data export in matlab alvian zainuddin
Data export in matlab alvian zainuddinData export in matlab alvian zainuddin
Data export in matlab alvian zainuddin
 
Experiment6matlab
Experiment6matlabExperiment6matlab
Experiment6matlab
 
Anilkumar1
Anilkumar1Anilkumar1
Anilkumar1
 
Index1 (2)
Index1 (2)Index1 (2)
Index1 (2)
 
Import/Export data in Matlab
Import/Export data in MatlabImport/Export data in Matlab
Import/Export data in Matlab
 
Basics of Image Processing using MATLAB
Basics of Image Processing using MATLABBasics of Image Processing using MATLAB
Basics of Image Processing using MATLAB
 
Step impulse
Step impulseStep impulse
Step impulse
 
Intro matlab and convolution islam
Intro matlab and convolution islamIntro matlab and convolution islam
Intro matlab and convolution islam
 
Matlab Importing Data
Matlab Importing DataMatlab Importing Data
Matlab Importing Data
 
MATLAB CODE OF FIR Filter Designing LPF HPF BPF BSF
MATLAB CODE OF FIR Filter Designing LPF HPF BPF BSFMATLAB CODE OF FIR Filter Designing LPF HPF BPF BSF
MATLAB CODE OF FIR Filter Designing LPF HPF BPF BSF
 
Brief Introduction to Matlab
Brief  Introduction to MatlabBrief  Introduction to Matlab
Brief Introduction to Matlab
 
Image processing
Image processingImage processing
Image processing
 
Matlab workshop
Matlab workshopMatlab workshop
Matlab workshop
 
IMAGE PROCESSING
IMAGE PROCESSINGIMAGE PROCESSING
IMAGE PROCESSING
 
Introduction to simulink (1)
Introduction to simulink (1)Introduction to simulink (1)
Introduction to simulink (1)
 
Matlab and Image Processing Workshop-SKERG
Matlab and Image Processing Workshop-SKERG Matlab and Image Processing Workshop-SKERG
Matlab and Image Processing Workshop-SKERG
 
Digital image processing using matlab (fundamentals)
Digital image processing using matlab (fundamentals)Digital image processing using matlab (fundamentals)
Digital image processing using matlab (fundamentals)
 
Image Processing Using MATLAB
Image Processing Using MATLABImage Processing Using MATLAB
Image Processing Using MATLAB
 
Simulink
SimulinkSimulink
Simulink
 
[Steven karris] introduction_to_simulink_with_engi
[Steven karris] introduction_to_simulink_with_engi[Steven karris] introduction_to_simulink_with_engi
[Steven karris] introduction_to_simulink_with_engi
 

Similar to Matlab data import

fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptxfINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptxdataKarthik
 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on rAbhik Seal
 
Unit 5 Time series Data Analysis.pdf
Unit 5 Time series Data Analysis.pdfUnit 5 Time series Data Analysis.pdf
Unit 5 Time series Data Analysis.pdfSheba41
 
Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...
Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...
Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...Kavika Roy
 
Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Barry DeCicco
 
Don't Repeat Yourself, and Automated Code Reviews
Don't Repeat Yourself, and Automated Code ReviewsDon't Repeat Yourself, and Automated Code Reviews
Don't Repeat Yourself, and Automated Code ReviewsGramener
 
Basics of MATLAB programming
Basics of MATLAB programmingBasics of MATLAB programming
Basics of MATLAB programmingRanjan Pal
 
Basics Of SAS Programming Language
Basics Of SAS Programming LanguageBasics Of SAS Programming Language
Basics Of SAS Programming Languageguest2160992
 
Pumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency AnalysisPumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency AnalysisUniversity of Illinois,Chicago
 
COMP41680 - Sample API Assignment¶In [5] .docx
COMP41680 - Sample API Assignment¶In [5] .docxCOMP41680 - Sample API Assignment¶In [5] .docx
COMP41680 - Sample API Assignment¶In [5] .docxpickersgillkayne
 
Pumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency AnalysisPumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency AnalysisUniversity of Illinois,Chicago
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using PythonNishantKumar1179
 
Unit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptxUnit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptxprakashvs7
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine LearningAmanBhalla14
 
Congrats ! You got your Data Science Job
Congrats ! You got your Data Science JobCongrats ! You got your Data Science Job
Congrats ! You got your Data Science JobRohit Dubey
 
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Yao Yao
 

Similar to Matlab data import (20)

fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptxfINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
fINAL Lesson_5_Data_Manipulation_using_R_v1.pptx
 
Data manipulation on r
Data manipulation on rData manipulation on r
Data manipulation on r
 
Unit 5 Time series Data Analysis.pdf
Unit 5 Time series Data Analysis.pdfUnit 5 Time series Data Analysis.pdf
Unit 5 Time series Data Analysis.pdf
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...
Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...
Unraveling The Meaning From COVID-19 Dataset Using Python – A Tutorial for be...
 
Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06Introduction to r studio on aws 2020 05_06
Introduction to r studio on aws 2020 05_06
 
Don't Repeat Yourself, and Automated Code Reviews
Don't Repeat Yourself, and Automated Code ReviewsDon't Repeat Yourself, and Automated Code Reviews
Don't Repeat Yourself, and Automated Code Reviews
 
Basics of MATLAB programming
Basics of MATLAB programmingBasics of MATLAB programming
Basics of MATLAB programming
 
Basics Of SAS Programming Language
Basics Of SAS Programming LanguageBasics Of SAS Programming Language
Basics Of SAS Programming Language
 
Pumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency AnalysisPumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency Analysis
 
COMP41680 - Sample API Assignment¶In [5] .docx
COMP41680 - Sample API Assignment¶In [5] .docxCOMP41680 - Sample API Assignment¶In [5] .docx
COMP41680 - Sample API Assignment¶In [5] .docx
 
Pumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency AnalysisPumps, Compressors and Turbine Fault Frequency Analysis
Pumps, Compressors and Turbine Fault Frequency Analysis
 
PPT on Data Science Using Python
PPT on Data Science Using PythonPPT on Data Science Using Python
PPT on Data Science Using Python
 
Sas classes in mumbai
Sas classes in mumbaiSas classes in mumbai
Sas classes in mumbai
 
Unit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptxUnit 4_Working with Graphs _python (2).pptx
Unit 4_Working with Graphs _python (2).pptx
 
Data Management in Python
Data Management in PythonData Management in Python
Data Management in Python
 
Data Management in R
Data Management in RData Management in R
Data Management in R
 
R programming & Machine Learning
R programming & Machine LearningR programming & Machine Learning
R programming & Machine Learning
 
Congrats ! You got your Data Science Job
Congrats ! You got your Data Science JobCongrats ! You got your Data Science Job
Congrats ! You got your Data Science Job
 
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
Mini-lab 1: Stochastic Gradient Descent classifier, Optimizing Logistic Regre...
 

More from pramodkumar1804 (20)

Matlab syntax
Matlab syntaxMatlab syntax
Matlab syntax
 
Matlab strings
Matlab stringsMatlab strings
Matlab strings
 
Matlab simulink
Matlab simulinkMatlab simulink
Matlab simulink
 
Matlab polynomials
Matlab polynomialsMatlab polynomials
Matlab polynomials
 
Matlab plotting
Matlab plottingMatlab plotting
Matlab plotting
 
Matlab overview 3
Matlab overview 3Matlab overview 3
Matlab overview 3
 
Matlab overview 2
Matlab overview 2Matlab overview 2
Matlab overview 2
 
Matlab overview
Matlab overviewMatlab overview
Matlab overview
 
Matlab operators
Matlab operatorsMatlab operators
Matlab operators
 
Matlab variables
Matlab variablesMatlab variables
Matlab variables
 
Matlab numbers
Matlab numbersMatlab numbers
Matlab numbers
 
Matlab matrics
Matlab matricsMatlab matrics
Matlab matrics
 
Matlab m files
Matlab m filesMatlab m files
Matlab m files
 
Matlab loops 2
Matlab loops 2Matlab loops 2
Matlab loops 2
 
Matlab loops
Matlab loopsMatlab loops
Matlab loops
 
Matlab integration
Matlab integrationMatlab integration
Matlab integration
 
Matlab graphics
Matlab graphicsMatlab graphics
Matlab graphics
 
Matlab gnu octave
Matlab gnu octaveMatlab gnu octave
Matlab gnu octave
 
Matlab operators
Matlab operatorsMatlab operators
Matlab operators
 
Matlab functions
Matlab functionsMatlab functions
Matlab functions
 

Recently uploaded

URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Recently uploaded (20)

URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

Matlab data import

  • 1. http://www.tutorialspoint.com/matlab/matlab_data_import.htm Copyright © tutorialspoint.com MATLAB - DATA IMPORT Importing data inMATLAB means loading data fromanexternalfile. The importdata functionallows loading various data files of different formats. It has the following five forms: S.N. Function and Description 1 A = importdata(filename) Loads data into array A fromthe file denoted by filename. 2 A = importdata('-pastespecial') Loads data fromthe systemclipboard rather thanfroma file. 3 A = importdata(___, delimiterIn) Interprets delimiterIn as the columnseparator inASCII file, filename, or the clipboard data. Youcan use delimiterIn withany of the input arguments inthe above syntaxes. 4 A = importdata(___, delimiterIn, headerlinesIn) Loads data fromASCII file, filename, or the clipboard, reading numeric data starting fromline headerlinesIn+1. 5 [A, delimiterOut, headerlinesOut] = importdata(___) dditionally returns the detected delimiter character for the input ASCII file indelimiterOut and the detected number of header lines inheaderlinesOut, using any of the input arguments inthe previous syntaxes. By default, Octave does not have support for importdata() function, so you will have to search and install this package to make following examples work with your Octave installation. Example 1 Let us load and display animage file. Create a script file and type the following code init: filename = 'smile.jpg'; A = importdata(filename); image(A); Whenyourunthe file, MATLAB displays the image file. However, youmust store it inthe current directory.
  • 2. Example 2 Inthis example, we import a text file and specify Delimiter and ColumnHeader. Let us create a space-delimited ASCII file withcolumnheaders, named weeklydata.txt. Our text file weeklydata.txt looks like this: SunDay MonDay TuesDay WednesDay ThursDay FriDay SatureDay 95.01 76.21 61.54 40.57 55.79 70.28 81.53 73.11 45.65 79.19 93.55 75.29 69.87 74.68 60.68 41.85 92.18 91.69 81.32 90.38 74.51 48.60 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 following code init: filename = 'weeklydata.txt'; delimiterIn = ' '; headerlinesIn = 1; A = importdata(filename,delimiterIn,headerlinesIn); % View data for k = [1:7] disp(A.colheaders{1, k}) disp(A.data(:, k)) disp(' ') end Whenyourunthe file, it displays the following result: SunDay 95.0100 73.1100 60.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 WednesDay 40.5700 93.5500 91.6900 41.0300 89.3600 ThursDay 55.7900 75.2900 81.3200 0.9900 13.8900 FriDay 70.2800 69.8700 90.3800
  • 3. 67.2200 19.8800 SatureDay 81.5300 74.6800 74.5100 93.1800 46.6000 Example 3 Inthis example, let us import data fromclipboard. Copy the following lines to the clipboard: Mathematics is simple Create a script file and type the following code: A = importdata('-pastespecial') Whenyourunthe file, it displays the following result: A = 'Mathematics is simple' Low-Level File I/O The importdata functionis a high-levelfunction. The low-levelfile I/O functions inMATLAB allow the most controlover reading or writing data to a file. However, these functions need more detailed informationabout your file to work efficiently. MATLAB provides the following functions for read and write operations at the byte or character level: Function Description fclose Close one or allopenfiles feof Test for end-of-file ferror Informationabout file I/O errors fgetl Read line fromfile, removing newline characters fgets Read line fromfile, keeping newline characters fopen Openfile, or obtaininformationabout openfiles fprintf Write data to text file fread Read data frombinary file frewind Move file positionindicator to beginning of openfile fscanf Read data fromtext file fseek Move to specified positioninfile ftell Positioninopenfile fwrite Write data to binary file
  • 4. Import Text Data Files with Low-Level I/O MATLAB provides the following functions for low-levelimport of text data files: The fscanf functionreads formatted data ina text or ASCII file. The fgetl and fgets functions read one line of a file at a time, where a newline character separates each line. The fread functionreads a streamof data at the byte or bit level. Example We have a text data file 'myfile.txt' saved inour working directory. The file stores rainfalldata for three months; June, July and August for the year 2012. The data inmyfile.txt contains repeated sets of time, monthand rainfallmeasurements at five places. The header data stores the number of months M; so we have M sets of measurements. The file looks like this: Rainfall Data Months: June, July, August M=3 12:00:00 June-2012 17.21 28.52 39.78 16.55 23.67 19.15 0.35 17.57 NaN 12.01 17.92 28.49 17.40 17.06 11.09 9.59 9.33 NaN 0.31 0.23 10.46 13.17 NaN 14.89 19.33 20.97 19.50 17.65 14.45 14.00 18.23 10.34 17.95 16.46 19.34 09:10:02 July-2012 12.76 16.94 14.38 11.86 16.89 20.46 23.17 NaN 24.89 19.33 30.97 49.50 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.50 47.65 24.45 34.00 28.67 30.34 27.95 36.46 29.34 15:03:40 August-2012 17.09 16.55 19.59 17.25 19.22 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 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 We willimport data fromthis file and display this data. Take the following steps: 1. Openthe file withfopen functionand get the file identifier. 2. Describe the data inthe file withformat specifiers, suchas '%s' for a string, '%d' for aninteger, or '%f' for a floating-point number. 3. To skip literalcharacters inthe file, include theminthe format description. To skip a data field, use an asterisk ('*') inthe specifier. For example, to read the headers and returnthe single value for M, we write: M = fscanf(fid, '%*s %*sn%*s %*s %*s %*snM=%dnn', 1); 4. By default, fscanf reads data according to our format descriptionuntilit cannot matchthe descriptionto
  • 5. the data, or it reaches the end of the file. Here we willuse for loop for reading 3 sets of data and eachtime, it willread 7 rows and 5 columns. 5. We willcreate a structure named mydata inthe workspace to store data read fromthe file. This structure has three fields - time, month, and raindata array. Create a script file and type the following code init: filename = '/data/myfile.txt'; rows = 7; cols = 5; % open the file fid = fopen(filename); % read the file headers, find M (number of months) M = fscanf(fid, '%*s %*sn%*s %*s %*s %*snM=%dnn', 1); % read each set of measurements for n = 1:M mydata(n).time = fscanf(fid, '%s', 1); mydata(n).month = fscanf(fid, '%s', 1); % fscanf fills the array in column order, % so transpose the results mydata(n).raindata = ... fscanf(fid, '%f', [rows, cols]); end for n = 1:M disp(mydata(n).time), disp(mydata(n).month) disp(mydata(n).raindata) end % close the file fclose(fid); Whenyourunthe file, it displays the following result: 12:00:00 June-2012 17.2100 17.5700 11.0900 13.1700 14.4500 28.5200 NaN 9.5900 NaN 14.0000 39.7800 12.0100 9.3300 14.8900 18.2300 16.5500 17.9200 NaN 19.3300 10.3400 23.6700 28.4900 0.3100 20.9700 17.9500 19.1500 17.4000 0.2300 19.5000 16.4600 0.3500 17.0600 10.4600 17.6500 19.3400 09:10:02 July-2012 12.7600 NaN 34.0000 33.1700 24.4500 16.9400 24.8900 18.2300 NaN 34.0000 14.3800 19.3300 30.3400 34.8900 28.6700 11.8600 30.9700 27.9500 29.3300 30.3400 16.8900 49.5000 16.4600 30.9700 27.9500 20.4600 47.6500 19.3400 49.5000 36.4600 23.1700 24.4500 30.4600 47.6500 29.3400 15:03:40 August-2012 17.0900 13.4800 27.2100 11.4500 25.0500 16.5500 22.5500 26.7900 13.4800 27.2100 19.5900 24.0100 24.9800 22.5500 26.7900 17.2500 NaN 12.2300 24.0100 24.9800 19.2200 21.1900 16.9900 NaN 12.2300 17.5400 25.8500 18.6700 21.1900 16.9900 11.4500 25.0500 17.5400 25.8500 18.6700