Import Data and plot it with an extra variable as vector

3 views (last 30 days)
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

Answers (2)

Jacob Wood
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)
  1 Comment
Lynna
Lynna on 18 Feb 2020
I don't really get how I divide or even initialize a vector/array or similar ^^ It's sth like: vector = 200 : 3 when i have 200 rows and 3 columns right? Or is it not important that its :3 because i just want 1 "new column" with the quotient in it?
And then my problem is I dont know how to get my data into it.
The Quotient is also no column of my table T so is it even possible so say T.quotient = x / y? Or should it be vectorQuotient = x / y?

Sign in to comment.


Steven Lord
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
  1 Comment
Lynna
Lynna on 19 Feb 2020
Yes the Import Data Tool is what i meant. And yes I use the Output Type "Table".
It's good to know that I just can kinda add a new column with a new parameter. I thought I needed to store it in an extra vector. Thanks for the explanation for this.
When I try your suggestion to just create a new T.Quotient "Column" with my wanted information stored I still get the Error Message about the same length.
I added my code (just 3 rows^^) and the error message. The code which is automatically created by Import Data Tool wasn't added 'cause I think Matlab would import it right and I didn't have a Problem here.
I don't know what I'm doing wrong here and I'm sorry it's kinda obvious for someone who uses matlab ^^
% other automatically created import code from the Import Data Tool
% Import the Data
t = readtable("C:\[...]\ImportedFileName.xlsx", opts, "UseExcel", false);
%%%% Code
%% Clear temporary variables (automatically created)
clear opts
% My Code:
t.quotient = t.production / t.consumption;
scatter(t.Date, t.quotient);
title('Verhältnis Gesamterzeugung zu Gesamtverbrauch über den Tag');
%%%% Error Message I get:
Error using scatter (line 78)
X and Y must be vectors of the same length.
Error in ScatterPlotQuotient (line 34)
scatter(t.Date, t.quotient);
% When I asked for their lenght:
>> length(t.quotient)
ans =
35040
>> length(t.Date)
ans =
35040

Sign in to comment.

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!