Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in matlab The readmatrix command is used to import data from excel. It is used in the format dataVariable = readmatrix('filename.xlsx', options...) where options can

in matlab

The readmatrix command is used to import data from excel. It is used in the format dataVariable = readmatrix('filename.xlsx', options...) where options can either be an object with optional settings or just a set of inputs to tell MATLAB how to format the data. By default, MATLAB will try to interpret the data the best it can but the option settings can be specifically set to represent what how the data is formated.

Options

Sometimes we'll have data with varying formats, different sheets, etc. We want to be able to instruct MATLAB how to interpret the data, the following is an example. We can either create an options object and set settings accordingly, or, if the data isn't too complex, we can just provide the options as "sets" of arguments.

For example, to specify the sheet to get the data from, we supply this pair of arguments 'Sheet', 'name_of_sheet' where the first defines what option, and the second provides the value. Then, to specify a range, we include this pair of arguments 'Range', 'the_range' where again, the first defines what option, and the second provides the value.

Let's run through an example.

% Read range A4:D28 from the sheet named 'OldResistors' resistors = readmatrix('Tutorial_04_1_Data.xlsx','Sheet','OldResistors','Range','A4:D28') % The following would be acceptable as well, notice the pair of options still stays together but the order of the pairs does not matter. %resistors = readmatrix('Tutorial_04_1_Data.xlsx','Range','A4:D28','Sheet','OldResistors')

Data Manipulation

Checking out the size of the data

Now, let's take a look at what was imported, use the size() function to determine the size of the variable. Remember you can use the search bar or help size in the command window for more information

resistorsSize= fprintf('%i rows and %i columns of data were read.\n\n', resistorsSize)

You may not have seen this notation for fprintf before, because resistorsSize is a vector, fprintf will start pulling out each element one by one from left to right to fill the values for the format specifiers.

If you look at the output of assigning resistorsSize you should notice it's a 1x2 array. The first column, as you might expect, is the number of rows.

There are a few rows and columns, they're all resistor values. In the following code, get the row and column count values from resistorsSize and multiply them to compute the total number of resistors.

resistorsCount=

Let's do some math with sum()

Create a vector with the sum for each column in the resistors array.

sumColumns=

Create a scalar that will have the total value of all resistors combined. Again, using the sum() function.

resistorsSum=

Getting the mean

Now, compute the total mean of the resistors' values.

resistorsMean=

Getting the Standard Deviation

Normally, with a vector this would be simple, but we have a 2D array and can't simply run the std() function on our data. There are a few ways of computing the std dev for a 2D array.

The long way

% We can compute our own standard deviation using a vectorized formula % step 1: Compute (x-xmean)^2 for each value in matrix resistors. deviations=(resistors-resistorsMean).^2; % Creates a matrix of deviations % step 2: sum all the deviations sumDevColumns=sum(deviations); % vector containing sum of each column sumDeviations=sum(sumDevColumns); % sum of the column vectors = sum(x-xmean)^2 % step 3: compute sqrt(sum(x-xmean)^2)/(N-1)) resistorsStd=sqrt(sumDeviations/(resistorsCount-1));

The slightly shorter way, let's just make our data into a vector and call std()

% If there were more than 4 rows, we may want to use a for loop resistorsStd2=std([resistors(1,:),resistors(2,:),resistors(3,:),resistors(4,:)])

The shortest way, instead of supplying a row or column, just use a colon operator to get all values in a single column vector.

resistorsStd3=std(resistors(:))

Output

To the command window

Let's output our data to the command window

fprintf('The resistors matrix has the following attributes\n') fprintf('The number of rows = %i\n', resistorsSize(1)) fprintf('The number of columns = %i\n', resistorsSize(2)) fprintf('The number of resistor values = %i\n', resistorsCount) fprintf('The sum of all resistor values = %g ohms\n', resistorsSum) fprintf('The mean of all resistor values = %g ohms\n', resistorsMean) fprintf('The std dev of all resistor values = %g ohms\n', resistorsStd)

To the Excel spreadsheet

Now store the data in the original Excel spreadsheet using writematrix (formerly xlswrite)

Let's put the values in as follows.

  • resistorsCount in cell H4 of sheet 'OldResistors'
  • resistorsSum in cell H5 of sheet 'OldResistors'
  • resistorsMean in cell H6 of sheet 'OldResistors'
  • resistorsStd in cell H7 of sheet 'OldResistors'

I'll do the first one, it's in the format writematrix(value, filename, ... sheet and range params). We should use variables so we don't have to repeat things like the filename, and sheet name

filename='Tutorial_04_1_Data.xlsx'; sheetName='OldResistors'; writematrix(resistorsCount,filename,'Sheet',sheetName,'Range','H4:H4') % Notify the user that data is written to the Excel file fprintf('These statistical values have also been written to Tutorial_04_1_Data.xlsx \n\n')

To a .mat binary file

Finally, let's try saving some variables to a .mat binary file. Check the slides, book, or help doc if you don't remember how to save workspace variables. Save the following workspace variables to a file named Tutorial_04_1.mat

  • resistorsSize
  • resistorsCount
  • resistorsSum
  • resistorsMean
  • resistorsStd

% Save some variables to Tutorial_04_1.mat % Notify the user that the variable have been saved fprintf('Workspace variables resistorsSize, resistorsCount, resistorsSum, resistorsMean,\n') fprintf('and resistorsStd have been saved to file Tutorial_04_1.mat\n\n')

Create a script of the same name, your output should match the following.

student submitted image, transcription available belowstudent submitted image, transcription available below

student submitted image, transcription available below  

resistors = 39.2874 89.7182 76.3553 59.5122 92.5087 69.1231 70.3836 66.4919 89.9239 100.9868 56.6764 91.3690 83.1586 92.6341 71.9937 80.9625 104.1922 85.9931 60.6651 66.3310 66.7734 71.4436 46.1142 90.2762 108.3383 60.6197 95.2304 69.2974 80.8930 86.4946 80.4885 98.7460 72.4920 55.5084 71.0505 79.4555 72.1948 82.8175 91.2509 82.3416 33.9167 102.3332 100.6288 47.7269 55.3930 61.8253 53.7050 70.8494 76.9302 61.2450 64.6193 91.8413 57.7472 46.4232 74.0763 86.8738 57.6960 55.0091 84.8620 66.0789 64.7370 44.9621 105.0038 85.7187 96.9735 84.2478 73.1242 93.7546 106.9110 87.2912 82.0327 67.9276 58.2082 84.0460 77.2512 80.5979 58.9763 69.7354 80.0637 66.0172 86.2757 72.4746 70.7277 80.6693 47.8282 51.4055 92.2017 71.6216 61.6914 86.4792 115.0607 74.3509 88.2254 63.4391 73.5871 78.4656 76.3369 97.6966 86.0036 93.0153

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Accounting Information Systems The Crossroads of Accounting & IT

Authors: Donna Kay, Ali Ovlia

2nd Edition

132991322, 978-0132991322

More Books

Students also viewed these Programming questions