Coverting "numeric" strings in cell to numbers

I have a some cells containing strings, some of which are numeric and some aren't. E.g. a = {'1' '2' 'xxx'} I'd like to convert the strings with numbers to a number format, leaving the others alone. a = {1 2 'xxx'}

 Accepted Answer

for i=1:numel(a)
if isnumeric(str2double(a{i})) & ~isnan(str2double(a{i}))
a{i}=str2double(a{i});
end
end

2 Comments

This runs str2double 3 times for each element. The test for a numeric output is not needed. Faster:
for k = 1:numel(a)
d = str2double(a{k})
if ~isnan(d)
a{i} = d;
end
end
The vectorized approach posted by Stephen is nicer and faster.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 4 Jan 2018
Edited: Stephen23 on 4 Jan 2018
Loops are not required, because str2double works extremely efficiently on a cell array of strings. Thus a more efficient solution uses simple MATLAB indexing:
>> a = {'1','2','xxx'};
>> v = str2double(a);
>> x = ~isnan(v);
>> a(x) = num2cell(v(x))
a = 1 2 'xxx'

7 Comments

But they are useful in some cases :)
"But they are useful in some cases :)"
Yes, they are useful in some cases. Just not this case: five lines of code, a superfluous loop, and multiple str2double are not an neat or efficient use of MATLAB. It is more complex than it needs to be, teaches inefficient usage of MATLAB, and is slow (1e4 iterations):
Elapsed time is 5.966 seconds. % your answer
Elapsed time is 1.611 seconds. % my answer
"Wish you do the same for other contributors' answers including loops."
I do.
"Whenever I answer a question by using loop, you always say this."
Perhaps because you keep writing inefficient loops?
"I suppose you live for moments like this."
I am sorry that you are hurt by some random stranger criticizing your code. Perhaps you might like to see this as a learning opportunity, and consider that these objective comments about your code might actually be objective comments about your code.
Note that when I make a criticism of any code (even my own) I ensure that I have clear factual statements, such as about the number of lines, the complexity, or quite often the runtime.
My aim on this forum (and I know this also applies to several other regular contributors) is to provide clear, neat, efficient solutions that are of lasting value to future browsers of this forum: I guess because that is what I would hope to find when I search some forum for solutions to my problems. My comments and factual statements are information for all future readers of this thread: I do not know what problems they might be working on but some of them are likely to have large data sets and huge computations, and I believe they deserve to be shown how easy it is to write efficient MATLAB code.
Birdman
Birdman on 4 Jan 2018
Edited: Birdman on 4 Jan 2018
Actually I learned so much comparing to when I began answering questions in forum, but of course I can not provide %100 efficient and neat solutions all the time. Instead writing it as a new answer, can't you just say under my answer for instance:
Birdman, that's is a nice working solution, but if you want to facilitate your code, you might want to check it out:
You do not do this, instead, I find it rude, you always criticize. I am open to criticism but of course in a polite way.
Furthermore, when I provide an efficient and neat solution, I would like to see your encouragement for me by upvoting or something else but I do not see that as well.
"Instead writing it as a new answer, can't you just say under my answer for instance:"
My answer was not an improvement on yours: if I had made improvements to your code then I certainly would have written this as a comment to your answer, just as Jan Simon did. My answer is totally independent to yours, and works in quite a different way. The code I gave surely deserves its own answer just as much as any one else's solution does: each can be voted for, discussed, criticized, or improved with its own comments.
I thought this is how this forum works.
"You do not do this, instead, I find it rude, you always criticize. I am open to criticism but of course in a polite way."
Until you commented on my answer and we started this discussion the complete text that I had written on this thread was this: "Loops are not required, because str2double works extremely efficiently on a cell array of strings. Thus a more efficient solution uses simple MATLAB indexing:"
I am struggling to find the part of that that you find to be rude. Please suggest how I could have written that more politely, whilst still being brief and factual.
"Furthermore, when I provide an efficient and neat solution, I would like to see your encouragement for me by upvoting or something else but I do not see that as well."
I often upvote neat and efficient solutions: when you write them I will be happy to upvote them. (I notice that you have not voted for my "efficient and neat solution". Does your rule for votes only apply to others, and not to yourself? You deserve "encouragement", but apparently I don't?).
Jan
Jan on 4 Jan 2018
Edited: Jan on 4 Jan 2018
@Birdman: Stephen did not criticize you, but he posted an answer and explained, why it is more efficient. He only shares his experiences and of course Stephen does this in other threads also. This is not rude or impolite in any way, but the purpose of this forum. I suggest not to take it personally.
"I suppose you live for moments like this." Whoops. This is off track.
Comments like "that's is a nice working solution, but..." are rare in this forum, because it is more efficient for the readers to get different solutions in separate answers. If a more efficient solution is hidden in the comments, they will get out of view, when more comments are posted.
Birdman
Birdman on 4 Jan 2018
Edited: Birdman on 4 Jan 2018
Okay guys, thanks for this discussion. I am happy that I talked about this here, so that I learned how you think about this situation. Thanks again.
Of course you deserve encouragement Stephen. I did not mean that. You understood what I tried to say.

Sign in to comment.

Categories

Find more on Language Fundamentals in Help Center and File Exchange

Asked:

on 4 Jan 2018

Edited:

on 4 Jan 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!