# 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.

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 {'B1472PQH'}

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.

>> strcmp (string_1, string_2);

>> strcmp ('ABC', 'ABC')

ans = logical 1

>> strcmp ('ABC', 'EBC') ans = logical 0

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

**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 = 7

**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.

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 end if (rows == hitung) && (tf == 0) hitung = 0; ulang = 0; //buat progam lainnya disini, lakukan sesuatu end end

- 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; disp("Benar"); end if (rows == hitung) && (tf == 0) hitung = 0; ulang = 0; disp("Salah"); end end

- 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.

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