How to Read a Column in Excel Using Matlab
- Download source code
Introduction
In the previous tutorial, we learnt:
- How to write data to an Excel file through Matlab?
- How to insert an Excel formula through Matlab?
- How to insert an image into an Excel sheet through Matlab?
- How to read information from an Excel File through Matlab?
As a follow-up to these manufactures, we will learn how to insert an Nautical chart into Excel Canvas through Matlab. At the end of this tutorial, you will learn how to open up an Microsoft Excel file, insert a chart, name the chart and the axes.
Steps
Stride 1: The first step is to create a COM server which runs the Excel Application.
Excel = actxserver(' Excel.Application');
This assumes that the Excel Application is installed in your system. If Excel is not installed, this argument will give an mistake. You can put the above statement within a endeavour-catch block:
try Excel = actxserver(' Excel.Application'); catch Excel = []; end
Step 2: In this step, we will open up an Excel file "ResultFile.xls" in the electric current directory.
ResultFile = [pwd ' \ResultFile.xls']; Workbook = invoke(Excel.Workbooks,' Open', ResultFile);
Step 3: Past default, the visibility of the Excel file is gear up to Simulated. You can make the Excel file visible using the command:
set(Excel,' Visible',1);
Instance
The above steps remain same for all the programs in Matlab dealing Microsoft Excel files. Let us assume, we accept recorded some readings from an experiment. (Encounter the attached screenshot). We have conducted 15 iterations. We have ii set of readings from the experiment. Theoretical and actual reading. Nosotros will encounter, how to plot these data as a function of the iteration value.
The attached screenshot shows the chart generated in Microsoft Excel. We volition encounter in this tutorial, how to programmatically create such a chart through Matlab.
Add a new chart
The first step is to add the nautical chart in the "ExperimentSheet
". And then, we are adding a new chart and we are going to rename the chart equally "ExperimentChart
". This can exist done using the following code :
Chart = Excel.ActiveSheet.Shapes.AddChart; %Let us Rename this chart to ' ExperimentChart' Chart.Name = ' ExperimentChart';
Delete the default entries in the chart :
By default, Excel chart would plot taking data from all the bachelor non-empty columns. And so, let us delete all the entries in the chart and offset with an empty chart. So, first we need to select the nautical chart 'ExperimentChart
'. Since nosotros take but 3 columns of data, we demand to call the invoke(Serial,'Delete')
thrice.
%% Delete Default Entries % Allow the states delete all the entries in the chart generated by defalut ExpChart = Excel.ActiveSheet.ChartObjects('ExperimentChart'); ExpChart.Activate; try Series = invoke(Excel.ActiveChart,'SeriesCollection',1); invoke(Series,'Delete'); Serial = invoke(Excel.ActiveChart,'SeriesCollection',one); invoke(Serial,'Delete'); Series = invoke(Excel.ActiveChart,'SeriesCollection',1); invoke(Serial,'Delete'); catch east end
Plot the entries
Now, all we have is a empty nautical chart. The get-go plot we are drawing is Experiment values in Column B as a function of Iteration value in Column A. So, first nosotros need to add a new plot to the ActiveChart. The X-values for this plot would be Column A and Y-Values would be Cavalcade B in the Excel sheet. In order to prepare the legend for this plot, we can take the header name for column B.
... %We are left with an empty nautical chart now. %Insert a Chart for Column B NewSeries = invoke(Excel.ActiveChart.SeriesCollection,'NewSeries'); NewSeries.XValues = ['=' resultsheet '!A' int2str(2) ':A' int2str(16)]; NewSeries.Values = ['=' resultsheet '!B' int2str(2) ':B' int2str(16)]; NewSeries.Name = ['=' resultsheet '!B' int2str(one) ]; ...
Similarly, nosotros can add some other plot in the chart and plot Column C every bit a function of Column A.
Nautical chart Style
Microsoft Excel support different chart fashion similar bar-graph, piechart, histogram to proper noun a few. So, for your instance, we are interested in having ii-D Line Graph. This can exist set using the following Matlab command:
Excel.ActiveChart.ChartType = ' xlXYScatterLinesNoMarkers';
The list of all supported chart style can exist found here.
Ready the axes
Now nosotros accept the desired plot. The only thing left is to name the axes. The 10-centrality, and the y-axis can exist gear up with the following code:
% Set up the x-axis Axes = invoke(Excel.ActiveChart,'Axes',1); set(Axes,'HasTitle',one); set(Axes.AxisTitle,'Caption','Experiment') % Fix the y-axis Axes = invoke(Excel.ActiveChart,'Axes',2); fix(Axes,'HasTitle',1); set(Axes.AxisTitle,'Caption','Results') %Give the Chart a championship Excel.ActiveChart.HasTitle = i; Excel.ActiveChart.ChartTitle.Characters.Text = 'Result vs Experiment';
Chart placement
You tin can skip this role, if yous think that the default placement of the chart in the Excel sail is not overlapping the data cells. And then, we volition look for an empty cell and place the chart starting from that particular jail cell. The Excel nautical chart placement needs 4 parameter:
- Width
- Height
- Left Placement
- Top Placement
%% Chart Placement Location = [ xlcolumn(2) int2str(20) ]; GetPlacement = become(Excel.ActiveSheet,'Range', Location); % Resize the Nautical chart ExpChart.Width = 400; ExpChart.Summit= 250; ExpChart.Left = GetPlacement.Left; ExpChart.Top = GetPlacement.Top;
Save Excel
The last stride is to save the Excel file and close the Excel application:
invoke(Excel.ActiveWorkbook,' Save'); Excel.Quit; Excel.delete; clear Excel;
Source: https://www.codeproject.com/Tips/536603/Insert-a-Chart-in-Excel-through-Matlab
0 Response to "How to Read a Column in Excel Using Matlab"
Post a Comment