Skip to content Skip to sidebar Skip to footer

Widget HTML #1

How to Compare Strings in Matlab From Excel Files

How to Compare Strings in Matlab From Excel Files

How to Compare Strings in Matlab From Excel Files - Previously we have learned how to read data from an Excel file per column and per cell using the "readcell" command.

Now from the data we will read per cell and compare the value in the cell with the value we have specified. We will compare this value as a String.

Cases like this will be very often encountered later, if you create a MATLAB project with a database, such as reading, filtering, comparing and determining the final result.

Let's say the excel file name is "database.xlxs" which contains the license plate data and the owner's name.

For an image of this database, please refer to the previous tutorial.


Membandingkan String Pada Matlab (Compare String In Matlab)
Compare String In Matlab

A. Reading Excel File Database Per Cell

The first step that must be done is to read the database per cell. This will be very important to do because it will save the program that we make.

Let's say the data we want to retrieve is in cell B2.

The way to program is to use the following command:

>> database = readcell('database.xlsx', 'Range', 'B2:B2')

Then the result of reading by matlab is as follows:

database =

  1×1 cell array


Here we have got the data "B1472PQH" in cell B2. This data is in the form of String data.

Next let's learn how to compare them.


B. Comparing String Data in Matlab

Comparing these strings will be very important for you to learn, because often using Matlab we create projects by comparing text to get the final result algorithm.

As an example:

  • If the input data is "123", then do plan "A".
  • If the input data is "001", then do plan "B".
  • If the input data is a "abc", then do plan "C".
  • and so on.
To compare strings in matlab, we use the "strcmp" command or read "string compare".
The basic command is:
>> strcmp (string_1, string_2);
If string_1 and string_2 are the same, then matlab will return the value 1.
Otherwise, matlab will return 0.
In our case example, we will compare the word "ABC" with "ABC":
>> strcmp ('ABC', 'ABC')
The results is:
ans =


If we compare the words "ABC" and "EBC", the result is:
>> strcmp ('ABC', 'EBC')

ans =


Now, from this logic 1 and 0 we can make a comparison program to take the next decision.

C. Read All Data From Excel and Compare the Data per Cell for Decision Making

The program that we make next is not as simple as above. Sometimes, there are more values ​​in the database, say 1000 rows.
How can we read directly from the database and compare the 1000 data with the data we have specified?
The trick is to use a loop, you can use "while" you can also use "for".
In this article we will use a loop using "while".
For those of you beginners who do not understand the while and For loops, I will briefly explain here.
While loop is a form of loop based on true and false conditions. If the condition is true, then the loop will continue to occur.
If the condition is false, the loop will stop.
For loop is a loop based on the number of conditions. For example, the conditions are 1 to 20, then the loop will occur 20 times.
If for 1 to 30, then the loop will occur 30 times.
Pretty clear huh?
Good. Now we will make a program using a loop to read as many rows of data as there are in the database.
How to?
First, let's read the database with specific columns, let's say only column B:
>> database =  readcell ('database.xlsx', 'Range', 'B1');
Second, we count the number of rows of data in column B, the command is:
>> rows = size(database, 1)

rows =

For example, the database.xlsx file has 7 data.
Third, read and compare the data 7 times. The number of repetitions 7 times will be done automatically, not in the program, not what we specify.
So the basic line of the program is:
ulang = 1;
hitung = 0;

while ulang
      hitung = hitung + 1;
      ambilData = database(hitung);
      tf = strcmp(ambilData, final_output);

      if tf == 1
         ulang = 0;

         //buat progam lainnya disini, lakukan sesuatu


      if (rows == hitung) && (tf == 0)
          hitung = 0;
          ulang = 0;
          //buat progam lainnya disini, lakukan sesuatu


The looping program above cannot be done in Windows Command, but we have to enter it into the matlab script with the .m extension, please make the script in matlab by:
  • Click "New"
  • Choose "Script"
  • Then a blank sheet will open, please copy the following program, then paste it into the script file earlier.

    database =  readcell ('database.xlsx', 'Range', 'B1');
    rows = size(database, 1);
    ulang = 1;
    hitung = 0;
    while ulang
        hitung = hitung + 1;
        ambilData = database(hitung);
        tf = strcmp(ambilData, "B1472PQH");
        if tf == 1
            ulang = 0;
        if (rows == hitung) && (tf == 0)
            hitung = 0;
            ulang = 0;
  • Save the script file with the extension .m.
  • Then create a database file using an excel file with the following data structure:

  • Then save the excel file together in a folder where the matlab file was located.
  • Name the excel file with the name "database.xlxs".
  • Then after that, run the matlab file by clicking "Run" which is on the top menu of matlab.
If the result that comes out in Windows Command is "true", it means that there is data "B1472PQH" in the database.
If it appears is "false", then the data being compared does not exist in the database.
Please replace the word "B1472PQH" in Matlab with another word. Then click "Run" again, you will see the result.
Hopefully this short article can help you in developing projects using Matlab.
Thank you for visiting the website Chip Piko.
If you find this article useful, please share it with your friends using the following share buttons:

Post a Comment for "How to Compare Strings in Matlab From Excel Files"