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

1 view (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 7 Dec 2022
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 Comments
Karl
Karl on 7 Dec 2022
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 on 7 Dec 2022
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')

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!