How to round values of table with mixed types?

13 ビュー (過去 30 日間)
Nicholas Kavouris
Nicholas Kavouris 2023 年 2 月 1 日
回答済み: Voss 2023 年 2 月 2 日
I would like to round all values in given columns, but get the error To assign to or create a variable in a table, the number of rows must match the height of the table.
I have attached a photo of the workspace variable of the table, only want to round certain rows as others have values which are unable to round
varnames={'Set Temp','duration','Start Temp','Time to Temp','Time to Steady','overshoot'};
for k=1:height(MasterTable)
for m=1:length(varnames)
varname=varnames{m};
MasterTable{k,varname}=mat2cell(round(cell2mat(MasterTable{k,varname})),3);
end
end
  2 件のコメント
dpb
dpb 2023 年 2 月 1 日
'Set Temp' appears to be integer-valued already excepting you've got some locations that contain an array instead of a single value. Why is that; that'll screw up trying to do anything with the variable. Fix the data design first, then you can simply write
t.(varname)=round(t.(varname));
and be done.
Nicholas Kavouris
Nicholas Kavouris 2023 年 2 月 2 日
Some cycles have more than one set temp, this is not a viable solution as this data design is necessary information to my programs outputs

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

回答 (2 件)

Steven Lord
Steven Lord 2023 年 2 月 2 日
Using a sample table:
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'});
T = table(Temp,Pressure,WindSpeed,WindDirection)
T = 3×4 table
Temp Pressure WindSpeed WindDirection ____ ________ _________ _____________ 37.3 30.1 13.4 NW 39.1 30.03 6.5 N 42.3 29.9 7.3 NW
Let's round all the numeric variables.
T2 = varfun(@round, T, 'InputVariables', @isnumeric)
T2 = 3×3 table
round_Temp round_Pressure round_WindSpeed __________ ______________ _______________ 37 30 13 39 30 7 42 30 7
We can see which variables from T were used in the varfun computation.
areNumeric = varfun(@isnumeric, T, 'OutputFormat', 'uniform')
areNumeric = 1×4 logical array
1 1 1 0
We could use those to overwrite the contents of T with T2. I'm going to make a backup copy of the original T for use in one more step.
T3 = T;
T(:, areNumeric) = T2
T = 3×4 table
Temp Pressure WindSpeed WindDirection ____ ________ _________ _____________ 37 30 13 NW 39 30 7 N 42 30 7 NW
If you only want to round some of the values, specify InputVariables differently.
v = [true false true false];
T4 = varfun(@round, T3, 'InputVariables', v); % leave Pressure alone
T3(:, v) = T4
T3 = 3×4 table
Temp Pressure WindSpeed WindDirection ____ ________ _________ _____________ 37 30.1 13 NW 39 30.03 7 N 42 29.9 7 NW

Voss
Voss 2023 年 2 月 2 日
If those columns that contain numeric values are cell arrays, as in
FW = ["EVT64"; "EVT64"; "EVT64"];
SetTemp = {[400;225]; [203;194]; 450};
Duration = {104.95; 264.15; 29.9833};
MasterTable = table(FW,SetTemp,Duration)
MasterTable = 3×3 table
FW SetTemp Duration _______ ____________ ____________ "EVT64" {2×1 double} {[104.9500]} "EVT64" {2×1 double} {[264.1500]} "EVT64" {[ 450]} {[ 29.9833]}
then you can modify your code as follows:
varnames = {'SetTemp','Duration'};
for k=1:height(MasterTable)
for m=1:length(varnames)
varname=varnames{m};
% MasterTable{k,varname}=mat2cell(round(cell2mat(MasterTable{k,varname})),3);
MasterTable.(varname){k} = round(MasterTable.(varname){k});
end
end
MasterTable
MasterTable = 3×3 table
FW SetTemp Duration _______ ____________ ________ "EVT64" {2×1 double} {[105]} "EVT64" {2×1 double} {[264]} "EVT64" {[ 450]} {[ 30]}

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by