0
votes

How we can remove ''' from beginning and end of strings in a cell array in MATLAB R2015a? Suppose that we have this cell array:

enter image description here

When we open one of cells we have this:

enter image description here

I want convert whole cell array to double (numbers). Suppose that out cell array is final. Using cellfun(@str2double,final) returns Nan for all cells. str2double(final) returns Nan too.

PS.

10 last elements of final in command prompt has this structure:

 ans = 
    ''2310''
    ''2319''
    ''2313''
    ''2318''
    ''2301''
    ''2302''
    ''2303''
    ''2312''
    ''2304''
    ''2309''
1
@rayryeng . Your proposed duplicate answer didn't work here. Any suggestion or hint? - Eghbal
Why does it not work? Update your post with your attempt and show us what went wrong. We aren't mindreaders. It would also be fruitful if you could post your data somewhere for us to take a look. - rayryeng
If you are getting NaN, then your cell array is not what you say it is. Please show us what the output looks like when you do final(1:20) for example in the MATLAB command prompt. Do not give us a screenshot of your workspace. - rayryeng
Thank you. post updated. - Eghbal
Reopened and I've placed an answer. Good luck! - rayryeng

1 Answers

2
votes

You can replace all of the apostrophe characters with nothing, then apply str2double to each cell in your cell array.

Given that your cell is stored in final, do something like this:

final_rep = strrep(final, '''', '');
out = cellfun(@str2double, final_rep);

Basically, use strrep to replace all of the apostrophe characters with nothing, then apply str2double to each cell in your cell array via cellfun.

Given your example above:

final = {'''2310'''
'''2319'''
'''2313'''
'''2318'''
'''2301'''
'''2302'''
'''2303'''
'''2312'''
'''2304'''
'''2309'''};

We now get this:

>> out =

        2310
        2319
        2313
        2318
        2301
        2302
        2303
        2312
        2304
        2309

>> class(out)

ans =

double

As you can see, the output of the array is double, as we expect.