Convert Columns Arrays to numeric

5 ビュー (過去 30 日間)
Stefan Azzopardi
Stefan Azzopardi 2020 年 1 月 15 日
回答済み: Steven Lord 2020 年 1 月 15 日
Hi,
I have a timetable with various data columns - I have imported these from an excel file. I need to make an addition to the all rows of colums and to add a column with the result. I think that my arrays are currently not numeric type and therefor I can not add them. Is there a way that I can convert data of columns 1-7 to numeric and make a summation? Or how can I know the data type of the columns?
1.PNG
  1 件のコメント
Andrei Bobrov
Andrei Bobrov 2020 年 1 月 15 日
Please attach here your variable 'combine' as mat-file
>> save('yourdata.mat','combine') % run in yur Command Window
And attach here file yourdata.mat .

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2020 年 1 月 15 日
編集済み: Andrei Bobrov 2020 年 1 月 15 日
...how can I know the data type of the columns?
varfun(@class,combine)
solution:
load('data.mat')
combine = [combine,rowfun(@funsum,combine,'OutputVariableName','SUM_ALL_kW')];
function out = funsum(varargin)
out = sum([varargin{:}],2);
end
or
combine.SUM_ALL_kW = sum(combine{:,:},2) ;
  2 件のコメント
Andrei Bobrov
Andrei Bobrov 2020 年 1 月 15 日
Comment by Stefan Azzopardi:
Andrei, Thanks for your help and it worked perfectly. This resulted in a new column with the sum of the other columns. Thank you once again for your help :) :)
Andrei Bobrov
Andrei Bobrov 2020 年 1 月 15 日
Stefan! If my answer solved your problem, then please accept it.

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

その他の回答 (2 件)

WalterWhite
WalterWhite 2020 年 1 月 15 日
t = datetime('now')
nn = datenum(t)+10;
f= datetime(nn,'ConvertFrom','datenum')
>> reading
t =
datetime
15-Jan-2020 09:52:43
f =
datetime
25-Jan-2020 09:52:43
did you mean something like this?
  1 件のコメント
Stefan Azzopardi
Stefan Azzopardi 2020 年 1 月 15 日
thank you for your help Walter, but it is not exactly what I meant. Anyway, I got all the help that I requested from Andrei :) :)

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


Steven Lord
Steven Lord 2020 年 1 月 15 日
Using the example from the timetable help text:
MeasurementTime = datetime({'2015-12-18 08:03:05';'2015-12-18 10:03:17';'2015-12-18 12:03:13'});
Temp = [37.3;39.1;42.3];
Pressure = [30.1;30.03;29.9];
WindSpeed = [13.4;6.5;7.3];
WindDirection = categorical({'NW';'N';'NW'});
TT = timetable(MeasurementTime,Temp,Pressure,WindSpeed,WindDirection)
Let's say that I wanted (for whatever reason) to add the temperature, pressure, and wind speed. I can extract that data from TT into a numeric array in two different ways:
A1 = TT{:, ["Temp", "Pressure", "WindSpeed"]} % Using variable names
A2 = TT{:, 1:3} % Using variable numbers
Now sum and put back into TT.
TT.combinedData = sum(A1, 2)
If all your data could be concatenated into an array with a uniform type, you could extract the data even easier. But this technique won't work for TT, as you can't combine a numeric variable like TT.Temp and a categorical variable like TT.WindDirection into one array.
TT.Variables

カテゴリ

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

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by