Convert cell array to array of strings

Jesh Kundem

I have imported some information from Excel, that's both strings and numbers. I read the Excel-file in MATLAB in raw format

Now, I have a column in a big matrix as

 [1]
 '1A'
 [2]
 [3]
 [4]
 [505]
 [601]
 [7]
 [8]

this is how the xlsread command gave it to me. I want to convert all these into strings like ['1' , '1A', '2' and so on..] .

I am struggling to do it. I want no blank spaces in each element. I mean it should not be like [ ' 1', ' 1A', ' 2' and so on]....

Stewie Griffin

Try to combine cellfun and num2str like this:

A = {1, '1A', 2, 3, 4, 505, 601, 7, 8}'
str = cellfun(@(c) num2str(c), A, 'UniformOutput', false).'
str = 

    '1'    '1A'    '2'    '3'    '4'    '505'    '601'    '7'    '8'

You can use num2str on stings, so you do not have to check if variable is a string or a numeric value. Note that the spaces in the result are between the elements, and not in the elements itself. So,

str(1)  
ans =    
    '1'    
str(2)    
ans =     
    '1A'

If your input data may contain spaces (your sample didn't), you can eliminate those by using an additional cellfun like this:

cellfun(@(c) c(c~=' '), str, 'UniformOutput', false) 

Side note: In most MATLAB functions, you can shorten parameter names, such as UniformOutput, false. This can be un, 0, uni, 0 ... etc, as long as the letters represent a unique parameter and can't mean something else. This is a nice trick if you write it in the command window, but I would avoid it in real code, as it could possibly break the code if The MathWorks determines to create a parameter called unit, UniformInput or something else starting with uni. Just a little tip =)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Convert Nokogiri XML Document into Array of Strings?

분류에서Dev

For ... in for an array of strings

분류에서Dev

parfor slicing of cell array

분류에서Dev

Filtering a Cell Array with Recursion

분류에서Dev

search in cell array in matlab

분류에서Dev

Iterate array in array and echo strings

분류에서Dev

Converting array of Strings to array of doubles

분류에서Dev

Join an array of strings

분류에서Dev

Creating an array of strings

분류에서Dev

Randomizing an array of C strings

분류에서Dev

Convert int[] array to ushort[] array

분류에서Dev

Convert an array of strings in the view Web Form View Engine format to javaScript Object

분류에서Dev

Convert jagged array to a list

분류에서Dev

Convert array to string Javascript

분류에서Dev

Convert Ruby Array into average

분류에서Dev

Convert Numpy array to image

분류에서Dev

Convert RGB array to HSL

분류에서Dev

Convert byte array to pdf

분류에서Dev

Convert Array into XML In PHP

분류에서Dev

Convert char array into double

분류에서Dev

Pickerview populated by array of strings adding to new array

분류에서Dev

How do I store an array of strings into another array of strings?

분류에서Dev

evaluating the array of strings using libsvm

분류에서Dev

Separating numbers to different strings or an array

분류에서Dev

Allocating memory to array of strings in c

분류에서Dev

Counting number of unique strings in array

분류에서Dev

Uploading Strings in an array of labels to parse

분류에서Dev

3D array of strings

분류에서Dev

Binding an NSTableView to an array of strings in Swift