Skip to main content

Diary of a Trainee Electronics Engineer: April 2018

MATLABPlot_ba2ea2745a4c9aaa3f2fba4add05e67c74f41d1d.png

Using MATLAB to read an .xls file for graphical user interface

For my Final Year Project at university I need to use MATLAB to process data in the graphical user interface (GUI), so in last month’s post I looked at what MATLAB can be used for and getting to grips with some of the basics.

Matlab_1_c3df2e526515ad051536b9d6523dbe5453fd8e8e.png

 

 

 

 

 

 

 

 

 

 

 

The image above shows basic calculations completed in MATLAB and then creating a variable, this was discussed in Diary of a Trainee Electronics Engineer: March 2018.

As I mentioned in a previous post, for my university final year project I will be looking at Energy Monitoring and Usage Management in Machine Tools. The aim of this project is to look at the energy consumed by a CNC machine via the various different processes. The tool used for this measurement is the Multicube950, using this the data can be downloaded in an .xls file.

Data1_5678627a1fdfd57a45c6399dcb2724fb98c7b725.png

The meter outputs many different readings such as:

  • Phase Volts
  • Line Volts
  • Amps
  • KW
  • KVA
  • KVAr
  • Amps Demand
  • Volts Demand
  • Peak Amps
  • Peak Volts
  • KW Demand
  • Power Factor …. Etc.

Not all the data which is outputted is relevant. For the purpose of this post we will look at comparing the KW against time to determine the amount of energy consumed at each stage. Since I would only be looking at these two columns of data (column B and L) I decided to temporarily delete column A and everything between and including C to K so I had the two columns I was looking at side by side.

When opening a file in MATLAB the first thing you need to do is make sure the file you want to open in MATLAB is saved in the MATLAB directory on your computer. For example, mine is – Chelsea > This PC > Documents > MATLAB.

I had to do some online reading to figure out what I needed to include in the script to output a graph from the data in the .xlsx file. To get started I needed to specify the name of the file which I wanted MATLAB to look at, to do this I used:

FileName = ‘Machine_Example.xlsx’

I then wrote a line which would instruct MATLAb to read the contents of the file which I stored as FileName.

A = xlsread(FileName);

Next, I needed to specify which columns in the file I wanted to look at and specify which axis I wanted each on, since removing the columns which I didn’t need for this my columns were the first and second ( A and B in excel). Since column A is time and B is the KW reading I wanted to plot time on the x-axis and the readings on the y-axis.

X = a(:, 1);

Y = a(:, 2);

If we look in the workspace section of MATLAB we can see that the file has been logged, a is set to a 6515x124 .xls(x) file and then X and Y are both 6515x1 which is correct as above I specified to include all the rows and one column for each. 

MATLAB_Workspace_123aa1e462a363dbea3beae0217bf1ad7b691ff6.png

 

 

 

 

 

 

Above we can see that we have set which columns in the file we want to use as the X and Y values. In this line we say:

The X axis will = read specified file = ‘:’ include all the rows, and column 1 (time);

The Y axis will = read specified file = ‘:’ include all the rows, and column 2 (KW);

MATLAB2_cb5a756b1e4d2c02488af1481a223345c55ffc4a.png

 

 

 

 

 

 

Since both X and Y have been specified we can use the ‘plot’ function to output a graph of this data.MATLABPlot1_ba2ea2745a4c9aaa3f2fba4add05e67c74f41d1d.png

After looking through the data I could see that the y-axis looked correct as well as the plot of the graph itself, however, the numbers along the x-axis didn’t make much sense to me. I figured this must be because of how the time was written in column 1. As the time was written as 12:03:45 it seems to have confused the formatting of the x-axis – I decided to recreate the graph from the same data to see what happened there.xls_Plot_0304ae05f261d4cee43915be253201ff575b4889.png

In the excel graph plot I can see along the x-axis here it has plotted the time as I expected it would have in MATLAB. I’m not quite sure why MATLAB has outputted the graph like this, I think it requires some more reading to get to the bottom of it.

Trainee Electronics Engineer, currently studying towards my degree in Electronic Engineering at the University of Hudderfsield. Completed my HND in Electrical & Electronic Engineering from Bradford College 2017. Love to try new things and build interesting projects!
DesignSpark Electrical Logolinkedin