how to convert cell to number in matlab

70 ビュー (過去 30 日間)
Putri Basenda Tarigan
Putri Basenda Tarigan 2020 年 11 月 17 日
回答済み: Rik 2020 年 11 月 17 日
Hello everyone,
I'm beginner in matlab. Could somebody help me on this:
I read my data as cell array from excel, where in one box in excel it contains two numbers, like 3;4
so it's read by matlab as c = {'3;4'}
I tried str2double function, but the result become NaN.
How to overcome this problem?
Thanks in advance.

回答 (1 件)

Rik
Rik 2020 年 11 月 17 日
str2double returns NaN because '3;4' is not a number, it is two numbers. You need to split the text into different numbers first. One of the many ways to do that is this:
c = '1,,-3.1;+4e2';%modified to cover more representations of numbers
%a number can only contain 0-9, a dot, a minus, a plus, or an E
%remove the * if you don't want multiple delimiters to be merged
split_values = regexp(c,'[^0-9\.\-+eE]*','split');
str2double(split_values)
ans = 1×3
1.0000 -3.1000 400.0000

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by