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

조회 수: 6 (최근 30일)
Christi Kruger
Christi Kruger 2021년 11월 10일
댓글: Jon 2021년 11월 10일
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
Christi Kruger
Christi Kruger 2021년 11월 10일
Operator '.*' is not supported for operands of type 'table'.
Error in FetalGrowthMatlab (line 6)
weighterr =weight.*0.1
Christi Kruger
Christi Kruger 2021년 11월 10일
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';

댓글을 달려면 로그인하십시오.

채택된 답변

Jon
Jon 2021년 11월 10일
편집: Jon 2021년 11월 10일
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
Jon
Jon 2021년 11월 10일
편집: Jon 2021년 11월 10일
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
Jon 2021년 11월 10일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by