How do I convert a column to numeric?

See the image
I want to count with the 1st column, but I always get the message "Undefined operator" - "for input arguments or type 'cell'." What can I do about it? I think the column is not numeric, but I am not sure. Who can help me?

8 件のコメント

Rik
Rik 2018 年 6 月 6 日
You should really consider a tutorial in Matlab. If you plan on using Matlab it is really worth it to invest the time in learning how to work with it. Have you tried cell2mat?
jakobjakob
jakobjakob 2018 年 6 月 6 日
Yes i tried it, but I couldn't calculate with it. It is something with 'double'. It is NOT numeric, cell array or string. I tested that with isstring etc. Can you help me?
Rik
Rik 2018 年 6 月 6 日
A double is a double precision floating point number: a numeric data type. Did you include the header text in the array you put into cell2mat?
Jan
Jan 2018 年 6 月 6 日
I tested that with isstring etc.
We cannot read your mind. What is "etc."? What did you try and what did you get as answer? Do you see that the decimal separator is a comma? Then this cannot be numeric values, because Matlab uses a dot. You did not mention, where you got this screenshot from.
jakobjakob
jakobjakob 2018 年 6 月 6 日
@Rik No I didn't include the header, only the numbers
jakobjakob
jakobjakob 2018 年 6 月 6 日
@Jan, it is a screenshot of an excelsheet, so that's why there are commas. In matlab there are dots. When I use the commando iscell it gives a '1'. Can you help me further?
jakobjakob
jakobjakob 2018 年 6 月 6 日
編集済み: jakobjakob 2018 年 6 月 6 日
I uploaded the variable, its about the variable ndata

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

 採用された回答

Stephen23
Stephen23 2018 年 6 月 6 日
編集済み: Stephen23 2018 年 6 月 6 日

2 投票

Where C is your cell array:
V = str2double(C(:,1))
This assumes that there is no header, and that each cell of the first column contains one number stored as a char (using a dot, not comma).

9 件のコメント

jakobjakob
jakobjakob 2018 年 6 月 6 日
Thanks, but when I do that, V is column with only NaN...
Stephen23
Stephen23 2018 年 6 月 6 日
@jakobjakob: then your description is not adequate for us to know what data you actually have. If you want further help with this please upload that variable in a .mat file by clicking the paperclip icon.
jakobjakob
jakobjakob 2018 年 6 月 6 日
I uploaded the variables, it is about the variable ndata
Stephen23
Stephen23 2018 年 6 月 6 日
編集済み: Stephen23 2018 年 6 月 6 日
ndata is already numeric. It has class double. You can check this using whos.
If you want to generate ndata from alldata, then the first column of alldata already contains numeric values, so you don't need to convert anything, just join then together into a numeric vector:
ndata = vertcat(alldata{:,1})
ndata = cell2mat(alldata(:,1))
jakobjakob
jakobjakob 2018 年 6 月 6 日
編集済み: jakobjakob 2018 年 6 月 6 日
Thanks, but my real problem is that I want to calculate with the first column of alldata. That is still not possible. For example: alldata(1,1) - 10. Can you help me with that?
Rik
Rik 2018 年 6 月 6 日
alldata(1,1) returns a cell. The ndata variable that either line of Stephens code produces will work with this syntax.
ndata = cell2mat(alldata(:,1));
alldata(1,1) - 10
jakobjakob
jakobjakob 2018 年 6 月 6 日
Thanks!
Stephen23
Stephen23 2018 年 6 月 6 日
編集済み: Stephen23 2018 年 6 月 6 日
@jakobjakob: The concept for indexing cell arrays is simple:
  • {} curly braces access the data inside the cells.
  • () parentheses access the cells themselves.
Just think of cell arrays like boxes: do you want to pick up the box (the cell, ()), or whatever that is inside the box (the data, {}). Sometimes you want to pick up the box, and sometimes you want to get out whatever is inside... each of these can be useful.
Therefore to access the contents of one cell you just need to use curly braces:
alldata{1,1} - 10
jakobjakob
jakobjakob 2018 年 6 月 6 日
Thanks a lot! Such a simple solution...

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

その他の回答 (0 件)

カテゴリ

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by