How to Build a Comma Delimited String in MATLAB

Matlab is a numerical computing environment which is particularly good for manipulating matrices. Matrices are also called arrays in this and other programming contexts. In Matlab, an array of numbers can be converted to a comma delimited string by changing its format. A comma delimited string is simply a list of numbers or other items separated by commas (but not spaces), as in 1,2,3,4. This is just one of many formats you can convert between in Matlab.

Instructions

    • 1

      Use the command sprintf(format, A, ...). "sprintf," as opposed to the usual "printf," which simply represents that a string is being printed.

    • 2

      Fill in the arguments of sprintf. Use '%.0f' for the format, which will take each element of the array, change it to a string and add a comma, and use the name of your array for A. The last argument is not necessary for printing a comma delimited string. All together, for array n, this looks like: newCommaDelimitedString = sprintf('%.0f,' , n);

    • 3

      Strip the final comma so that your list does not look like: 1,2,3,4,. Do this by simply taking off the last character, reducing the length by one. This looks like: newCommaDelimitedString = newCommaDelimitedString = newCommaDelimitedString(1:end-1);

Related Searches:

References

Comments

Related Ads

Featured