Import Data and plot it with an extra variable as vector
5 views (last 30 days)
Show older comments
Hello Community,
so I'm quite new to Matlab and I got some beginner struggle:
I imported an Excel sheet with "3" columns (Date, electricity consumption, electricity production) over the function "Import Data". And I want to initialize a variable "quotient" that has the quotient of "production / consumption" for each row. Now i just want to plot (scatter) this quotient with the date. The Errors told me "Vectors must be the same length" so I figured out I need this variable as a vector, but honestly I don't get how to proper initialize a vector and add every quotient for every row to it.
I picture sth like this:
Q = initilize Vector Q
for length of table
Q = production / consumption
end
scatter(table.Date, Q)
I hope someone can help me with this minor problem (:
Thanks
0 Comments
Answers (2)
Jacob Wood
on 18 Feb 2020
Should be an easy fix!
What you are looking for is an element-wise divide: https://www.mathworks.com/help/matlab/ref/rdivide.html
After you have imported your data into a table "T" your code might look like this:
T.quotient = T.production ./ T.consumption;
scatter(T.Date,T.quotient)
Steven Lord
on 18 Feb 2020
Since you reference the "Import Data" 'function' I assume you're using the Import Tool opened by clicking the button labeled "Import Data" in the Variable section on the Home tab of the MATLAB Desktop's toolstrip? The one with a green arrow pointing down into a rectangle?
If so, and if you selected to import the data into a table array using the "Output Type" dropdown (in the Imported Data section of the Import tab of the Import Tools toolstrip) then you've read the data into a table array already.
If you have your data in a table array you can reference variables in the table array by typing <name of the table>.<name of the variable>. Let's take the table created by the "Store Related Data Variables in Table" example on the table documentation page. I'm using this code instead of interactively importing a file because it's easier than showing a series of pictures of the steps to import the data using the Import Tool.
LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'};
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T = table(LastName,Age,Smoker,Height,Weight,BloodPressure)
The table T has six variables. If I wanted to compute (as an example) each person's weight (stored in the variable Weight in T) divided by their height (the variable Height in T):
y = T.Weight ./ T.Height
If I wanted to store that as a new variable in the table T:
T.WeightOverHeight = T.Weight ./ T.Height
See Also
Categories
Find more on Data Distribution 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!