Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
MATLAB - Data Types
Next article icon

Set Variable Data Types in MATLAB

Last Updated : 26 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

There are many cases when a user has to import data into MATLAB script from various files. These file types could be .txt, .xlsx, .csv, .dat, etc. types. Now, each file type has its own method of defining data types. However, for computation purposes, MATLAB requires the data to be of numeric type, rather than character. Thus, there is a requirement to set the data types of these imported data. MATLAB solves this problem by providing the setvartype function to change the data type of a given variable from the imported data. In this article, we shall see the usage of the same function with the help of some examples.

Syntax:

<list_of_variables> = setvartype(<list_of_variables>, selection, type)

The first argument to the function is the list of imported data variables. The selection arguments allows setting the data type of selected variables from list_of_variables. Lastly, the type argument takes the data type to be set for the selected variables. If the selection argument is omitted then, setvartype changes the data type of all variables passed in the list.

Example 1:

We shall import the following data into our MATLAB script to illustrate the usage of setvartype.

 

To import this excel spreadsheet, we shall use the detectImportOptions function.

Matlab
% MATLAB Code dat = detectImportOptions("data.xlsx");  % Displaying the variable names and  % their data type using the dat object disp([dat.VariableNames ;dat.VariableTypes]) 

Output:

Above code imports the data.xlsx file and displays its variable names and their types.

 

As can be seen that first variable is of character type, the second one is of double and last one is a logical type. Now we shall change all three of these variables to string type using the setvartype function.

Matlab
% MATLAB Code dat = detectImportOptions("data.xlsx"); disp("Changing type to string!")  % Changing all variables to string type dat = setvartype(dat,'string'); disp([dat.VariableNames ;dat.VariableTypes]) 

Output:

As there is no selection of variables made in the above code, setvartype will change all the variables in dat.

 

Example 2

Now we shall use the same initial data file and change the data type of selected variables by passing the optional selection parameter.

Matlab
% MATLAB Code dat = detectImportOptions("data.xlsx"); disp("Changing selected types!")  % Changing numeric_data to single type dat = setvartype(dat,{'numeric_data'},'single');  % Changing numeric_data to 8-bit integer dat = setvartype(dat,{'logical_data'},'int8');  disp([dat.VariableNames ;dat.VariableTypes]) 

Output:

In the above code, we change the 'numeric_data' to single and the 'logical_data' to int8 type as it contains Boolean data. The output will be:

 

Next Article
MATLAB - Data Types

O

owl0223
Improve
Article Tags :
  • Technical Scripter
  • MATLAB
  • Technical Scripter 2022
  • MATLAB-programs

Similar Reads

  • Variable Names in MATLAB
    A variable is a named-memory location that stores different types of data which can be used to perform a specific set of operations. It can be thought of as a container that holds some value in memory. The Matlab workspace store all the variables being used during a session. This workspace not only
    5 min read
  • Set environment variable in MATLAB
    Setting environment variables with the help of setenv() function. The setenv() function is used to set the specified value of an operating system environment variable. Here the environment variable "name" will be replaced with "value", where "name" and "value" is its parameters. Syntaxsetenv(name, v
    1 min read
  • Write Data to Text Files in MATLAB
    Writing data to a text file means creating a file with data that will be saved on a computer's secondary memory such as a hard disk, CD-ROM, network drive, etc. fprintf() function is used to write data to a text file in MATLAB. It writes formatted text to a file exactly as specified. The different e
    3 min read
  • MATLAB - Data Types
    MATLAB is a platform which provides millions of Engineers and Scientists to analyze data using programming and numerical computing algorithm and also help in creating models. Data types are particular types of data items defined by the values they can store in them, generally, in programming languag
    5 min read
  • Global Variables in MATLAB
    Variables in programming are generally storage spaces to store a certain type of data. There are many types of variables, but two commonly used types are local and Global variables. Generally, each MATLAB function has its own local variables. But sometimes for the sake of programming, we need to cha
    4 min read
  • Normalize Data in MATLAB
    Data Normalization is a technique in statistical mathematics that converts the entire data into a specified range or scale or normalizes it using different methods such as by computing its z-score. There is no specific definition of normalization but, it has various meanings depending on the user's
    2 min read
  • Clear variable from Memory in MATLAB
    Clearing variables from memory, with the help of clearvars operation. The clearvars operation is used to clear the specified variables from memory or from the currently active workspace. Syntax:clearvars variables clearvars -except keepVariables Parameters: This function accepts a parameter. variabl
    1 min read
  • Variables and Data Types in VBA Excel
    In a computer system, variables and data types are almost used in every program to store and represent data. Similarly, Excel VBA also has variables and data types to store and represent data and its type. In this article, we will learn about VBA variables, their scope, data types, and much more. VB
    9 min read
  • List Variables in Workspace With Sizes and Types in MATLAB
    MATLAB works with workspaces, which contain all variables, and their metadata. This makes accessing data in large codes easy. However, sometimes there are requirements for listing all the variables in the MATLAB terminal or properties of some particular variables.  Fortunately, MATLAB provides a met
    2 min read
  • Data Types in Statistics
    Data is a simple record or collection of different numbers, characters, images, and others that are processed to form Information. In statistics, we have different types of data that are used to represent various information. In statistics, we analyze the data to obtain any meaningful information an
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences