How do I convert strings stored in a cell array to numbers?

193 ビュー (過去 30 日間)
Alex
Alex 2011 年 2 月 5 日
コメント済み: Stephen23 2020 年 2 月 27 日
I have an array with strings in it - the format is the following:
'Name' '2/8' '3/7' '7/8'
Each of the above is its own cell within the array - i think this is called a cell array (i have not dealt with strings in matlab before)
What i want is the same matrix, except I want to remove the '' in each cell, and I want to actually DO the divisions. i.e '2/8' becomes 0.25 etc
how do i exract the name from 'Name', put it in the first column, then the next columns become the actual values rather than just a string containing characters.
P.S I am not putting the '' in the example for this post, it is actually there in the cell.
  2 件のコメント
Peter Strassmann
Peter Strassmann 2020 年 2 月 27 日
For a conversion without divisions, there are much faster ways of conversion:
Stephen23
Stephen23 2020 年 2 月 27 日
For a conversion with divisions, there are much faster ways of conversion than the accepted answer:

サインインしてコメントする。

採用された回答

Oleg Komarov
Oleg Komarov 2011 年 2 月 5 日
編集済み: John Kelly 2015 年 2 月 27 日
Hi Alex, you cannot mix double values with char values, but you still have to use a cell array (or drop the header 'Names').
% Suppose your input is:
c = {'Name' '2/8' '3/7' '7/8'}
EDIT (from Walter Roberson syntax, didn't know str2num behavior with operators) :
% Then your output would look like this:
[c(1); cellfun(@str2num,c(2:end),'un',0).']
ans =
'Name'
[0.2500]
[0.4286]
[0.8750]
Note that the ' are just there tell you that you're using a cell array of strings, but the actual content of each cell doesn't have them at all.
For more info Cell Arrays
Oleg

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2011 年 2 月 5 日
You asked to extract the name and put it in the first column and that the rest of the columns would be actual values. Because of your use of the verb "extract" for the name, it sounds to me as if you might not be aware that it is not possible to have an ordinary array that has a mix of characters and numeric values. In order to mix characters and numeric values, a cell array must be used.
If C is your cell array,
[C(:,1) cellfun(@str2num, C(:,2:end))]
  1 件のコメント
Walter Roberson
Walter Roberson 2011 年 2 月 5 日
Note: in my answer, str2num will take care of doing the divisions.

サインインしてコメントする。


Stephen23
Stephen23 2020 年 2 月 27 日
Here is a faster solution (which also does not rely of the evil eval hidden inside of str2num):
>> C = {'Name','2/8','3/7','7/8'};
>> V = sscanf(sprintf(' %s',C{2:end}),'%f/%f',[1,Inf]);
>> Z = V(1:2:end)./V(2:2:end)
Z =
0.25 0.42857 0.875
And comparing the times (1e4 iterations):
Elapsed time is 3.360732 seconds. % Slow CELLFUN with STR2NUM
Elapsed time is 0.576273 seconds. % Fast SPRINTF with SSCANF

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by