How to Import Data from a CSV File with Numeric Values and Texts into MATLAB Workspace?


CSV or Comma Separated Values is a file format that can store numeric and text values. The most important characteristic of a CSV file is that it allows to exchange the data among a variety of sources.

MATLAB is a powerful tool that provides a large number of built-in functions to perform various operations. It provides the following two functions that enable the users to import data with numeric and text values from a CSV file into the MATLAB workspace.

  • readtable()

  • importdata()

In this tutorial, I will show you the process of importing data from a CSV file into the MATLAB workspace using these functions.

Import Data Using the Readtable() Function

In MATLAB, the "readtable" function is a built-in function in its function library. This function is used to import data with text and numeric values into the MATLAB workspace.

Syntax

data = readtable(file_path);

The step-by-step process of importing data with numeric values and text from a CSV file into MATLAB workspace is explained below.

  • Step (1) − Read the CSV file and store in a variable.

  • Step (2) − Specify the variable names.

  • Step (3) − Call the "readtable" function to import data from CSV file.

  • Step (4) − Display the imported data.

We can follow these simple four steps to import data from a CSV file into the MATLAB workspace.

Example

Let us take an example to understand the implementation of these steps in MATLAB programming.

% MATLAB code to create a sample CSV file

% Create a sample table with numeric and text data
course_data = table([101; 102; 103; 104; 105; 106; 107;], {'Math'; 'Physics'; 'Chemistry'; 'MATLAB'; 'Electronics'; 'C++'; 'Java';}, [1000; 1500; 1200; 1500; 450; 700; 600], 'VariableNames', {'CourseID', 'CourseName', 'Fee'});

% Specify the name for the CSV file
file_name = 'course_data.csv';

% Write the table to the CSV file
writetable(course_data, file_name);

% Display the confirmation message and table
disp('The CSV file has been created successfully.');
disp(course_data);

Output

It will create a CSV file with the following data −

The CSV file has been created successfully.

    CourseID      CourseName       Fee 
    ________    _______________    ____

      101       {'Math'       }    1000
      102       {'Physics'    }    1500
      103       {'Chemistry'  }    1200
      104       {'MATLAB'     }    1500
      105       {'Electronics'}     450
      106       {'C++'        }     700
      107       {'Java'       }     600

Now, let us implement the code to import the numeric values and text data from this CSV file into the workspace.

Example

% Import data from CSV file into workspace using readtable function
% Specify the file path to the CSV file
file_path = '/MATLAB Drive/course_data.csv';	% Replace with your CSV file path

% Call the readtable function to import data from the CSV file
data = readtable(file_path);

% Display the imported data
disp('This CSV file contains the following data.');
disp(data);

Output

When you run this code, it will produce the following output

This CSV file contains the following data.

    CourseID      CourseName       Fee 
    ________    _______________    ____

      101       {'Math'       }    1000
      102       {'Physics'    }    1500
      103       {'Chemistry'  }    1200
      104       {'MATLAB'     }    1500
      105       {'Electronics'}     450
      106       {'C++'        }     700
      107       {'Java'       }     600

This example shows how we can use the "readtable" function to import data from a CSV file into MATLAB workspace.

Let us see another method of importing data with numeric values and text from a CSV file into MATLAB workspace.

Import Data Using the Importdata() Function

In MATLAB, the "importdata" function is another built-in function in MATLAB function library. This can be used to import numeric values and text data from a CSV file into the MATLAB workspace.

Syntax

data = importdata(file_path, delimiter);

The steps involved in importing the numeric values and text data from a CSV file into the workspace using the "importdata" function are explained below.

  • Step (1) − Read the CSV file and store in a variable.

  • Step (2) − Call the "importdata" function to access the data in the CSV file.

  • Step (3) − Read and display the column heaters.

  • Step (4) − Read and display the text data in the CSV file.

  • Step (5) − Read and display the numeric data in the CSV file.

Example

Let us see an example to implement these steps in MATLAB programming.

% Import data from CSV file into workspace using importdata function
% Specify the file path to the CSV file
file_path = '/MATLAB Drive/course_data.csv'; % Replace with your CSV file path

% Call the importdata function to read the CSV file
data = importdata(file_path);

% Read and display the text data
text_data = data.textdata;
disp('Text Data:');
disp(text_data);

% Read and display the numeric data
numeric_data = data.data;
disp('Numeric Data:');
disp(numeric_data);

Output

When you run this code, it will produce the following output

Text Data:
    {'CourseID'}    {'CourseName' }    {'Fee'   }   Numeric Data:
    {'101'     }    {'Math'       }      {0×0 char}           1000
    {'102'     }    {'Physics'    }      {0×0 char}           1500
    {'103'     }    {'Chemistry'  }      {0×0 char}           1200
    {'104'     }    {'MATLAB'     }      {0×0 char}           1500
    {'105'     }    {'Electronics'}      {0×0 char}            450
    {'106'     }    {'C++'        }      {0×0 char}            700
    {'107'     }    {'Java'       }      {0×0 char}            600


In this example, I demonstrated the use of the "importdata" function to import numeric values and text data from a CSV file into the MATLAB workspace.

Conclusion

In conclusion, importing data with numeric values and text from a CSV file into the workspace is one of the basic and crucial operation in MATLAB. CSV files are used to store data comma-separated values.

In this tutorial, I explained the step-by-step process of importing numeric and text data from a CSV file into workspace using two built-in functions in MATLAB. These functions are "readtable" and "importdata". You can try these MATLAB codes with different CSV files by replacing the file path.

Updated on: 06-Oct-2023

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements