How can I properly change a row in my matrix table in excel into letters?

1 次查看(过去 30 天)
nexample = [150, 1, 176, 20, 2000
200, 2, 181, 18, 2300 ]
150, 1, 168, 17, 1900
190, 2, 182, 30, 2400]
nlabels = [{'Weight (lbs) ','Sex','Height (cm)','Age','Calorie Consumption'}];
nexcel = array2table(nexample,'VariableNames', nlabels);
writetable(ncxcel,'example-sheet.xls')
Hello all! I am trying to scan the columns in row 2 and change the numbers to: 1 - Male and 2 - Female so it will show in my excel instead of numbers. What is the best method for doing this? These variables will change depending on user input, so for now this is what I came up with
for col = 1:1:4
if nexample(col,2) == 1
nchange = char(77);
elseif nexcample(col,2) == 2
nchange = char(70); % i would use all the characters if it worked
end
end
Any help is appreciated, i understand it's probably impossible because they aren't the same class. But how do I scan and change it and then replace it with row 2 of the excel sheet?

采纳的回答

Walter Roberson
Walter Roberson 2022-12-7
Sx = categorical({'Male', 'Female'});
nexcel.('Height (cm)') = Sx(nexcel.('Height (cm)'));
I think people are going to find it somewhat odd to see Height listed as Male or Female...
  3 个评论
Karl
Karl 2022-12-7
clc
clear
close all
nexample = [150, 1, 176, 20, 2000;
200, 2, 181, 18, 2300;
150, 1, 168, 17, 1900;
190, 2, 182, 30, 2400]
nlabels = {'Weight (lbs)', 'Sex' ,'Height (cm)','Age','Calorie Consumption'};
nexcel = array2table(nexample,'VariableNames', nlabels);
Sx = categorical({'Male', 'Female'});
nexcel.('Sex') = Sx(nexcel.('Sex'));
writetable(nexcel,'example-sheet.xls')
I'm getting an error "To assign to or create a variable in a table, the number of rows must match the height of the table." I'm not sure what this is referring to.
Walter Roberson
Walter Roberson 2022-12-7
nexample = [150, 1, 176, 20, 2000;
200, 2, 181, 18, 2300;
150, 1, 168, 17, 1900;
190, 2, 182, 30, 2400]
nexample = 4×5
150 1 176 20 2000 200 2 181 18 2300 150 1 168 17 1900 190 2 182 30 2400
nlabels = {'Weight (lbs)', 'Sex' ,'Height (cm)','Age','Calorie Consumption'};
nexcel = array2table(nexample,'VariableNames', nlabels);
Sx = categorical({'Male', 'Female'});
nexcel.Sex = reshape(Sx(nexcel.Sex), [], 1);
nexcel
nexcel = 4×5 table
Weight (lbs) Sex Height (cm) Age Calorie Consumption ____________ ______ ___________ ___ ___________________ 150 Male 176 20 2000 200 Female 181 18 2300 150 Male 168 17 1900 190 Female 182 30 2400
writetable(nexcel,'example-sheet.xls')

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by