A blog to help electrical engineers and students to learn electrical engineering topics. Basic Electrical Engineering, Power Systems, Electrical Machines, tec.. Now special course on MATLAB for Beginners.

Class

Class

Sunday, August 28, 2016

MATLAB Programming Tips Part 1



MATLAB program (functions and scripts) are stored as script file(.m files). This type of file contains MATLAB commands, so running it is equivalent to typing all the commands—one at a time—at the Command window prompt. You can run the file by typing its name at the Command window.


Matlab Programming Tips Part 1 from Shameer Ahmed Koya
  • Both functions and scripts are stored in .m files
  • —Type up a bunch of commands and save as filename.m
  • —Type filename in command window to run
  • Example: first_program.m
  • —The name of a script file must begin with a letter, and may include digits and the underscore character, up to 63 characters.
  • —Do not give a script file the same name as a variable.
  • —Do not give a script file the same name as a MATLAB command or function.

Tuesday, August 23, 2016

Polynomials and Curve Fitting in MATLAB



MATLAB provides a number of functions for the manipulation of polynomials. Polynomials are defined in MATLAB as row vectors made up of the coefficients of the polynomial, whose dimension is n+1, n being the degree of the polynomial. Polynomials in MATLAB vector must include all polynomial coefficients, even those that are zero.

MATLAB provides the function polyval to evaluate polynomials. MATLAB does not provide a direct function for adding or subtracting polynomials unless they are of the same order, when they are of the same order, normal matrix addition and subtraction applies. Polynomial multiplication is supported by the conv function. Where we want to divide one polynomial by another, in MATLAB we use the deconv function.

Polynomials and Curve Fitting in MATLAB from Shameer Ahmed Koya

Curve fitting is the process of adjusting a mathematical function so that it lays as closely as possible to a set of data points. MATLAB provides a number of ways to fit a curve to a set of measured data. One of these methods uses the “least squares” curve fit. This technique minimizes the squared errors between the curve and the set of measured data. The function polyfit solves the least squares polynomial curve fitting problem.

Friday, August 5, 2016

Working with Excel files in MATLAB - Part 2



Read part 1 here. We will discuss few more functions to deal with Excel files.

xlswrite ( ) – Write to Microsoft Excel spreadsheet file

xlswrite is a simple function to write data to an excel worksheet. The syntax is similar to the xlsread function
xlswrtie(‘filename’, A, ‘worksheet’, ‘range’)
The function write the values in the array A to the specified range of cells in the specified worksheet of the excel file. The array may be a numeric array, character array or a cell array depending on the data. The worksheet and range arguments are optional. If not specified, the data will be written to the first worksheet starting from A1.

Example:
No we will add marks of two more students to our grade file.
>> newstud = {342001, 'James', 12.5, 13, 20;342001, 'Anna', 12, 10.5, 16};


>> xlswrite('test.xlsx', newstud, 'A7:E8')

writetable( ) - Write table to file

wrtietable function by default write the content of martlab table to a text file in comma separated format. The file name will be automatically set if not specified.
writetable(T) is the basic syntax.
writetable(T, ‘filename’) writes the table T to the file as column-oriented data.  The function automatically determines the file format based on the extension specified in the filename.
Optional arguments to select the work sheet and range is available when you write to an excel file.
Example:
We will write the contents of the table T from the previous example to the second sheet of the same file.
>> writetable(T,'test.xlsx','sheet',2)
The command will write the data to the Sheet2.

Importdata( ) - Load data from file

importdata is used to load data from a file to the workspace. The function will recognise the file type from the extension and will call appropriate procedure to load the data.
A = importdata(‘filename’) loads data into the structure A.
Example:
>> A = importdata('test.xlsx')
A =
          data: [1x1 struct]
      textdata: [1x1 struct]
    colheaders: [1x1 struct]
To get the contents use the commands
A.data
A.textdata
A.colheaders