Use data from excel, input them into an equation in matlab that creates the points to be plotted in Matlab

9 views (last 30 days)
Hello,
I am brand new with matlab and I want to take a bunch of data from excel and put it into matlab. Using that data, I want to multiple two columns to create the y axis and use one of the columns as the x axis.
Here is an image of the data I have where column a is the time for the x axis and column b and c are multiplied to create y axis component for power in and column d and e are multiplied to create y axis for power out.
Here is an example of the chart I am trying to recreate. You can see that there are dots for power in and power out and they are charted on the same plot.
The second part is to create a plot that looks like the image below. The equation is efficiency=(power in)/(power out)
I understand that i have to put the xls file in the matlab folder I am working with and to pull it into the workspace with "xlsread testfile" but I have no idea where to go from there. It's temping to just use excel but I have to do the math in Matlab.
Thank you for any help you can provide! -Erin

Accepted Answer

dpb
dpb on 30 Sep 2016
Edited: dpb on 3 Oct 2016
"...i have to put the xls file in the matlab folder I am working with..."
No, not really; you simply have to provide a fullpathname filename to xlsread. It's sometimes easier to have things in a local working directory, sure, but it surely isn't mandatory.
Once you've read in the values, then you'll have the columns in an array...multiplying two columns in Matlab is such a joy as compared to Excel where you have to duplicate everything over and over and over and over again... :(
data=xlsread('yourfile.xls');
t=data(:,1); % convenient short variable
data(:,1)=[]; % take time out of the data array
Pi=prod(data(:,1:2),2); % same as writing data(:,1).*data(:,2)
Po=prod(data(:,3:4),2); % and other two columns
*ERRATUM:* Can't use *scatter* on more than one vector at a time
% scatter(t,[Pi Po]); % a plot scatter plot
scatter(t,Pi,30,'b','filled'); % a plot scatter plot
hold on % to add to existing axes
scatter(t,Po,30,'r','filled'); % a plot scatter plot
*END ERRATUM*
xlim([fix(t(1)) round(t(end))] % set axes limits to beginning of day and next
datetick('x','keeplimits') % set time display keeping overall limits
legend('P_{in}','P_{out}','location','best')
Your other computations are probably written nearly as succinctly...
NB: I'm presuming xlsread still returns the Matlab datenum in using datetick; with newer releases of Matlab than I have you can use some newer features for time data/plots. Se the doc for datetime class.
  4 Comments
Erin Momany
Erin Momany on 3 Oct 2016
Edited: Erin Momany on 3 Oct 2016
Ooh its so beautiful!! It actually works!
I have more questions if you don't mind. Am I able to create the second plot in the same script? The second plot is taking the Pi/Po= efficiency value as the y axis and the time as the x axis. It is also a line plot, I think. I was reading about the hold on command and am not sure how to create two plots in the same script ( or if its possible ).
dpb
dpb on 3 Oct 2016
Of course; Matlab is a full-feature programming language, you can do essentially anything you wish... :)
figure % create a new figure
I think I'll let you figure out (so to speak :) ) from the previous example how to compute the efficiency and plot it--it takes only two code lines since you have the two P variables already. Note that "element-wise" math operators in Matlab use the "dot" operators; that'll be important. I showed the alternative syntax in comment in previous answer for multiplication; only difference here is division.
I suggest opening the documentation and working through the earlier tutorials on syntax and its introduction to basic math and graphics; you'll get along much more quickly if you don't have to rely on somebody to come along and answer every little question first.
PS: If Answer solves the problem, please go ahead and "ACCEPT" it so other folks know the issue is basically resolved...thanks.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!