How to import data from excel and multiply a specific extracted column?
7 views (last 30 days)
Show older comments
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
Accepted Answer
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
on 10 Nov 2021
Edited: Jon
on 10 Nov 2021
Your welcome. MATLAB tables are really nice. They more or less provide a direct representation of what you have in your Excel worksheets, but you can work on them in MATLAB.
Note for future reference, it isn't really necessary to even create the intermediate variables like weight, and weightdata. You could use the table variable directly in your expressions, e.g
weighterr =m.Weight_lbs_*0.1
In that case though you might want to first clean up the variable names in the Excel, so they don't have the awkward _lbs_ at the end of them. You can also rename table variables after you read them in if that's easier.
Finally, one small point, but good to know. You don't need the .* to multiply a vector and a scalar, you can just use *
The .* does an element by element multiplication, but this is only needed if multiplying two vectors of the same length.
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
More Answers (0)
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!