How can I convert data in format cell to double?

15 ビュー (過去 30 日間)
Xinyuan Wei
Xinyuan Wei 2016 年 6 月 20 日
回答済み: Xinyuan Wei 2016 年 6 月 21 日
How can I conver the data in format cell attached here to double? I tried function double. It works well for some of my data but not this file. I cannot not understand what is the difference.

採用された回答

Star Strider
Star Strider 2016 年 6 月 20 日
You have to reference ‘G3’ specifically, then convert it.
This works:
d = load('Xinyuan Wei X.mat');
X = d.X;
G3 = X.G3; % Define ‘G3’
G3_dbl = str2num(cell2mat(G3)); % Convert ‘G3’ To Double
This is quite definitely not obvious! I had to experiment with it to get it to a double array.
  2 件のコメント
Guillaume
Guillaume 2016 年 6 月 20 日
I would advise against str2num as this evaluate arbitrary expressions in the string (including ones that wipe the hard drive!).
G3_dbl = str2double(G3);
is simpler anyway (does not need cell2mat)
Star Strider
Star Strider 2016 年 6 月 20 日
Excellent point!
Thank you for the clarification. I didn’t consider that.
(I always read your and Andrei Bobrov’s Answers and Comments because I learn so much from them. Thank you for your continuing contributions to MATLAB Answers.)

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

その他の回答 (3 件)

KSSV
KSSV 2016 年 6 月 20 日
It is because..if you see your first dataset in X it is 'G3'. As it is a string, MATLAB is showing error to convert to double. You delete the first dataset 'G3' then double shall work.
  1 件のコメント
Xinyuan Wei
Xinyuan Wei 2016 年 6 月 20 日
</matlabcentral/answers/uploaded_files/54599/QQ%E6%88%AA%E5%9B%BE20160620212113.png> When I try to remove the varname 'G3', it gives out this error. Could you tell how to do this in detail? Thank you for your help.

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


Shameer Parmar
Shameer Parmar 2016 年 6 月 20 日
As the 'X' is in dataset format, so you need to create new variable by fetching data for 'X'.
first load your .mat file, then run the following code..
for i = 1: length(X)
x(i) = str2num(X{i,1});
end
x = x';

Xinyuan Wei
Xinyuan Wei 2016 年 6 月 21 日
Thank you so much for all your help.

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by