Want to read take the values in an excel file and calculate the equation

35 views (last 30 days)
I am creating a code that reads an Excel file's values line by line then calculates the spectral line intensity equation using the values of each line of the Excel sheet. After taking the calculation, it moves on to the next line and does the same thing in a loop. I am trying to figure out how to tell Excel to read one line at a time, use the values of that line to calculate the equation, and then move on to the next and then store that calculation. This is what I have so far:
clear all
sdata = readtable('3057-3058.xlsx', 'Range', 'B1:K34')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
sdata = 33×10 table
v S A yair yself E_ nair x_air J_ J__1 ______ _________ _________ _____ _____ ______ ____ ______ __ ____ 3057.2 2.892e-25 0.05955 0.045 0.059 1780.1 0.6 -0.01 19 18 3057.2 2.237e-25 0.04603 0.045 0.059 1780 0.6 -0.01 19 18 3057.2 7.842e-26 0.0003846 0.047 0.065 950.3 0.62 -0.01 14 13 3057.2 1.331e-27 5.231e-05 0.046 0.061 1417.1 0.61 -0.01 17 16 3057.3 6.537e-25 0.2403 0.044 0.069 1639 0.75 -0.006 5 4 3057.3 1.052e-24 0.09416 0.047 0.094 1487.8 0.75 -0.006 6 5 3057.4 1.974e-23 0.1451 0.039 0.065 950.13 0.62 -0.01 14 13 3057.4 3.328e-29 0.0001196 0.045 0.057 2397.8 0.58 -0.01 22 21 3057.4 2.958e-23 0.145 0.047 0.065 950.14 0.62 -0.01 14 13 3057.5 1.225e-26 0.0001137 0.047 0.065 1095.3 0.62 -0.01 15 14 3057.5 5.301e-25 0.2012 0.032 0.055 1674.5 0.75 -0.006 9 8 3057.5 4.503e-23 0.1324 0.048 0.065 950.15 0.62 -0.01 14 13 3057.5 2.395e-25 0.03688 0.044 0.068 1459.9 0.75 -0.006 5 5 3057.5 6.816e-25 0.1724 0.044 0.069 1674.4 0.75 -0.006 9 8 3057.6 1.379e-24 0.198 0.047 0.099 1640.2 0.75 -0.006 8 8 3057.6 7.132e-26 0.09447 0.046 0.058 2183.1 0.59 -0.01 21 20
Ia = 9.88274*10^-1;
p = pi;
c = 2.998*10^10;
g1 = 1;
c2 = 1.4387769;
T = 296;
Q = 5.9053*10^2;
for k = 1:length(sdata)
S = (Ia)*(A / (8*(p)*(c)*(v)^2))*(((g1)*exp(1))*(1 - (exp(1))/(Q)));
end
Error using tabular/length
Undefined function 'LENGTH' for input arguments of type 'table'. Use the height, width, or size functions instead.
  1 Comment
Dyuman Joshi
Dyuman Joshi on 12 Sep 2022
Use size() instead of length as the error (clearly) suggests.
You have written a loop but there is no loop index in the for loop code? And it is just overwriting the variable S.

Sign in to comment.

Answers (1)

Pulkit
Pulkit on 15 Sep 2022
As per my understanding, you want to read data from an EXCEL file, perform some calculation and then write output data back to the EXCEL file. Here you are using values of variable A and v from the Excel sheet. All other variables have constant values.
  • I also see that you have used ‘readtable’ function. Please refer the following documentation link to know more about accessing variables from table
  • Also ‘length’ function is not defined for table type instead use ‘size’ function. Please refer to the following documentation link
Here you can achieve this as follows:
clear all;
sdata = readtable('3057-3058.xlsx', 'Range', 'B1:K34','VariableNamingRule','preserve');
Ia = 9.88274*10^-1;
p = pi;
c = 2.998*10^10;
g1 = 1;
c2 = 1.4387769;
T = 296;
Q = 5.9053*10^2;
v=sdata.v; % storing value of v from sdata table
A=sdata.A; % storing value of A from sdata table
% with for loop as you mentioned
for k = 1:size(sdata)
S(k,1) = (Ia)*(A(k) / (8*(p)*(c)*(v(k))^2))*(((g1)*exp(1))*(1 - (exp(1))/(Q)));
end
% you may also achieve this in vector form without use of loop(method2)
S_new=(Ia)*(A(:) ./ (8*(p)*(c)*(v(:)).^2))*(((g1)*exp(1))*(1 - (exp(1))/(Q)));
Refer the following documentation for Right array division and element wise power operation
Please refer the following documentation for writing data to excel sheet.
Hope this answers your question.

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!