xlsread to retrieve information based on input

Hello!
I am working on a chemical engineering calculator of sorts and need to retrieve data from a sizable excel sheet. I have a number of function files that needs different information from the sheet.
The first column is all component names, and the following columns have info like critical pressure and temp, heat capacity constants, etc.
Is there a way for xlsread to search the component column for a particular string (ie "water" or "ethanol") and so i can get the properties for just the component of interest (user specified)?
Or is using xlsread just the wrong way to go about what I'm trying to do?
Many thanks!

Answers (2)

If the data sheet is not extremely large, you can use xlsread() to read in all the data and then process it.
[Num,Txt,Raw]=xlsread(YouExcelFile);
Assume Raw is below:
Raw={'water',1,2,3;'ethanol',10,20,30};
Index=strcmp(Raw(:,1),'water');
FindValue=Raw(Index,:);
FindValue =
'water' [1] [2] [3]

5 Comments

Its a 468x33 sheet haha
Im thinking use xls read to an array 'data' have user input something for component, using a loop to search 'data' for a match of said input in that column. I have to think the code out, but i think the idea will work?
"Sizable"? That's microscopic. Just read the whole thing in and don't worry about it - your computer will barely even notice something that tiny read into memory.
It should work. Use ComName=input('prompt','s') to get a sting input. use strcmpi() if it is not case sensitive. You don't need a loop to search. Just use Index=strcmp() above. Take a look at the value of Index and you'll understand how it works.
Thanks for the help!
I see it adds a 1 where the strings match, but I'm trying to use Index as a coordinate to look up a value in the data array. Heres a piece of code:
if calculation == 3
n = strcmpi(Data,component)
component = n;
[A, B, C, D] = Cp_Constants(component);
end
______________________________________________________________
function [A B C D] = Cp_Constants(component)
%function gets heat capacity constants of a gas
A = data(component,16);
B = data(component,17);
C = data(componet,18);
D = data(component,19);
end
__________________________________________________________
n returns an array of the same size, I just want to know what row of Data it found the matching string
If you want the linear index, instead of the logical index, you can use
n=find(strcmpi(Data(:,1),component)),

Sign in to comment.

I got it working , kinda:
if calculation == 3
for n = 1:469
comp = Data(n,4);
if strcmp(comp,component) == 1
component = n;
[A, B, C, D] = CpConstants(component);
end
end
end
The function file is returning: ??? Undefined function or method 'Data' for input arguments of type 'double'.
Error in ==> CpConstants at 4 A = Data(component,16);
Heres the function again:
function [A, B, C, D] = CpConstants(component)
%function gets heat capacity constants of a gas
[num, txt, Data] = xlsread('Data.xlsx');
A = Data(component,16);
B = Data(component,17);
C = Data(component,18);
D = Data(component,19);
end
This usually means the files are not in the same directory though, correct? If I just type A = Data(component,16) in the command window, it works fine. The script file and function are in the same directory, checked using the 'which' function on both, I tried adding the xlsread into the function file to make sure it has the array to read, and still nothing.
Am I missing something painfully obvious? I have been up awhile after all

2 Comments

Earlier you were referring to an array named "data" and now you refer to "Data" ??
I changed them all to Data after I posted.
I realized that the function was saving to another folder, and even though I cut/pasted it to the same folder as the script, I had to do save-as to the folder the script was in.
Thanks all

Sign in to comment.

Categories

Asked:

on 10 Dec 2011

Community Treasure Hunt

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

Start Hunting!