How to import data from excel and multiply a specific extracted column?

7 views (last 30 days)
Christi Kruger
Christi Kruger on 10 Nov 2021
Commented: Jon on 10 Nov 2021
I am trying to extract data from excel and use it to calculate a 10 percent error that will be used later in code. My code works if I manually enter the data into a row vector, but I want to use the readtable or xlsread funtion to extract the data from excel instead. This is my code but it keeps giving me an error that the operator * is not found. Do I need to transpose it first?
m=readtable('Fetal Growth Data.xlsx','ReadVariableNames',0);
weight = readtable('Fetal Growth Data.xlsx','Range','B2:B14')
weightdata = readtable('Fetal Growth Data.xlsx','Range', 'C2:C14')
weeks = [12:24]
weighterr =weight.*0.1
% Put the bar plot under the errorbar plot
p1 = bar(weeks,weightdata,"g");
p2=errorbar(weeks,weight,weighterr,'-ob');
title("Fetal Growth Development")
xlabel("Weeks")
ylabel("Weight (lbs)")
legend("weight","location","northwest")
xticks([12:24])
yticks([0.1:0.1:1.5]);
% find the bars to be colored red
idx = find(weightdata < (weight - weighterr));
p1.CData(idx,:) = repmat([1 0 0], [numel(idx), 1]);
p1.FaceColor='flat';
  4 Comments
Christi Kruger
Christi Kruger on 10 Nov 2021
m=readtable('Fetal Growth Data.xlsx','ReadVariableNames',0);
weight = readtable('Fetal Growth Data.xlsx','Range','B2:B14')
weightdata = readtable('Fetal Growth Data.xlsx','Range', 'C2:C14')
weeks = [12:24]
weighterr =weight.*0.1
% Put the bar plot under the errorbar plot
p1 = bar(weeks,weightdata,"g");
p2=errorbar(weeks,weight,weighterr,'-ob');
title("Fetal Growth Development")
xlabel("Weeks")
ylabel("Weight (lbs)")
legend("weight","location","northwest")
xticks([12:24])
yticks([0.1:0.1:1.5]);
% find the bars to be colored red
idx = find(weightdata < (weight - weighterr));
p1.CData(idx,:) = repmat([1 0 0], [numel(idx), 1]);
p1.FaceColor='flat';

Sign in to comment.

Accepted Answer

Jon
Jon on 10 Nov 2021
Edited: Jon on 10 Nov 2021
Hi,
The problem is that you are reading the variables into tables, not vectors. You can't multiply a scalar value times a table.
You need to get the table columns into vectors and then do the multiply.
You can just use your first call to read all of the data into one table (but set ReadVariableNames true) and then extract the individual columns like this (I commented out your code that is no longer needed) I
Note that readtable has restrictions on acceptable names for variables so it converts the original names Weight(lbs) and Weightdata(lbs) to Weight_lbs_ and Weightdata_lbs_
m=readtable('Fetal Growth Data.xlsx','ReadVariableNames',1); % set ReadVariableName true
% % % weight = readtable('Fetal Growth Data.xlsx','Range','B2:B14')
% % % weightdata = readtable('Fetal Growth Data.xlsx','Range', 'C2:C14')
weight = m.Weight_lbs_
weightdata = m.Weightdata_lbs_
weeks = [12:24]
weighterr =weight.*0.1
% Put the bar plot under the errorbar plot
p1 = bar(weeks,weightdata,"g");
p2=errorbar(weeks,weight,weighterr,'-ob');
title("Fetal Growth Development")
xlabel("Weeks")
ylabel("Weight (lbs)")
legend("weight","location","northwest")
xticks([12:24])
yticks([0.1:0.1:1.5]);
% find the bars to be colored red
idx = find(weightdata < (weight - weighterr));
p1.CData(idx,:) = repmat([1 0 0], [numel(idx), 1]);
p1.FaceColor='flat';
  4 Comments
Jon
Jon on 10 Nov 2021
To change variable names you can do this:
m.Properties.VariableNames = {'weeks','weight','weightdata'}
then you could just refer to them for example as
weighterr = m.weight*0.10

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!