# How do i convert a numerical cell array in a vector ?

4 ビュー (過去 30 日間)
Davide Conti 2019 年 10 月 29 日
コメント済み: Guillaume 2019 年 10 月 30 日
I have a data table imported from Excel with 304 rows and 75 columns.
At the position {10,30} for example i have a cell with comma delimitation like this:
Arr = {'5,6,16,17,19,21,25,27,28'}
I would like that in the position {10,76} of my matrix there is the same string of numbers converted into row vector.

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

### 採用された回答

Guillaume 2019 年 10 月 29 日
A safer alternative to str2num would be:
sscanf(Arr{1}, '%d,') %assuming all the numbers are integers, otherwise '%f'
##### 2 件のコメント表示非表示 1 件の古いコメント
Guillaume 2019 年 10 月 30 日
You already have a number in the 2nd row (as opposed to textual representation of numbers). There's nothing to do there.
However, note that it's rarely a good idea to mix actual numbers and text in the same container. You will have to test whether each row is actually textual or numeric before using it.
If you wanted to convert that whole column, you could use a loop as follows:
for row = 1:size(yourcellarray, 1)
if ~isnumeric(yourcellarray{row, somecolumnindex})
yourcellarray{row, somecolumnindex} = sscanf(yourcellarray{row, somecolumnindex}, '%d,').';
end
end

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

### その他の回答 (1 件)

Sebastian Bomberg 2019 年 10 月 29 日
Use str2num on the content of the cell:
str2num(Arr{:})
##### 1 件のコメント表示非表示 なし
Guillaume 2019 年 10 月 29 日

I would be very careful with using str2num on arbitrary input coming from an excel file. str2num is a dangerous function that will happily eval whatever text it is passed so somebody could easily craft an excel file that would make str2num format the hard drive or other nastiness.
e.g. on window see the effect of:

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

### カテゴリ

Find more on Data Type Conversion in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by