# Solved: create csv of strings

Creating a CSV file of strings is a common problem faced by many data analysts and data scientists. Child’s play for coding professionals, this task often finds itself steeped in complexity for those less familiar with programming languages. Thankfully, high-level languages like Matlab, specifically designed for numerical computing, make this process quite straightforward and simple to implement even for the regular users. This article is all about leveraging the power and usability of Matlab to create CSV files with ease.

## The Core Problem: Creating a CSV file of strings in Matlab

Matlab, developed by MathWorks, does not limit its prowess to just numerical computing. It proves to be a powerful tool when it comes to data analysis, creation, and manipulation. One often faced challenge is to create a CSV file of strings. CSV, Comma-Separated Values, files are simple files that contain tabular data.

These can be easily imported into programs like Excel for further manipulation. Matlab, with its rich library of commands and functions, provides a neat solution to the problem. Specifically, the function of interest here is writetable.

## The Solution: Matlab’s writetable Command

The writetable command is a built-in Matlab function specifically designed for writing table data to a file. To create a CSV file of strings, we first need to create a table of strings.

```% Create a cell array of strings
strArray = {'Paris'; 'London'; 'New York'; 'Tokyo'; 'Beijing'};

% Convert the cell array to a table
T = table(strArray);

% Write the table to a CSV file
writetable(T, 'cities.csv');
```

The first line of the code creates an array of strings with the familiar city names. This is converted into a table in the next line. The resulting table is then written to a CSV file named cities.csv.

## Step-by-step Explanation

• The first step involves creating an array of strings. In Matlab, this can be achieved using curly brackets {}. In the example above, the array consists of names of some prominent cities across the globe.
• The code to do this is as straightforward as it gets.

```% Create a cell array of strings
strArray = {'Paris'; 'London'; 'New York'; 'Tokyo'; 'Beijing'};
```
• The table function in Matlab converts this array of strings into a table. Tables are a more efficient way of handling data, especially when dealing with large datasets.
• This is what it looks like in Matlab.

```% Convert the cell array to a table
T = table(strArray);
```
• The final step is to write this table to a CSV file. The writetable function in Matlab accomplishes this for us.
• And here’s how we do it,

```% Write the table to a CSV file
writetable(T, 'cities.csv');
```

That’s it! The CSV file has successfully been created with the mentioned string data.

In conclusion, Matlab, with its rich set of commands and functions, has proved to be highly efficient for the creation of CSV files, therefore ensuring a significant reduction in the complexity of the task for regular users. With this newfound knowledge, creating a CSV file with strings should now be a cakewalk!

Related posts: