Want to read take the values in an excel file and calculate the equation
9 views (last 30 days)
Show older comments
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')
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
1 Comment
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.
Answers (1)
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.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!