1

I have following cell array of strings:

daycell = 

'd100'
'd104'
'd105'

I would like to make an array of numbers from it like this:

array =

100   104   105

I can accomplish it using loop:

daycell = {'d100';'d104';'d105'}
array = [];
for g = 1:1:length(daycell)
    array(g) = str2double(daycell{g}(2:end));
end

Is is possible to do it without loop? Is is even better using it without loop? The daycell has much more rows in real.


So far I am able to eliminate the d letter using:

daym = cell2mat(daycell);
daynoD = daym(:,2:end);

daynoD =

100
104
105

but I do not know what to do with the matrix of characters.

3
  • 4
    You can use str2num(daynoD). Commented Jan 7, 2013 at 14:37
  • @H.Muster ahh..true, I tried str2double but it is not that clever function, thx Commented Jan 7, 2013 at 15:12
  • There is a difference between str2num and str2double! Commented Jan 7, 2013 at 15:27

1 Answer 1

4

One way is to use cellfun

cellfun(@(x)str2double(x(2:end)),daycell);
Sign up to request clarification or add additional context in comments.

1 Comment

Nice one, Matlab has so many useful functions that it is difficult to keep track on them. .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.