How do I have MATLAB read an Excel file, interpolate between table values, and then assign the values to variables?
5 views (last 30 days)
Show older comments
I am trying to make a MATLAB code for calculating the convective heat transfer coefficent (h) for air based on temperatures that I input. I'm new and don't know much about using MATLAB, so I'm having some trouble. Since h depends on a lot of other values (kinematic viscosity, thermal conductivity, Prandtl number, etc), I made tables in Excel (on different sheets) which have the first column as temperature and the second as the corresponding value (i.e. temperature vs kinematic viscosity on the first sheet, temperature vs. thermal conductivity on the second sheet, etc.). What I'd like to do is input two temperatures into the code, then have MATLAB take the average of the temperatures and use it to search through the Excel tables to find the values of viscosity, conductivity, etc. that correspond to the calculated temperature. Then I need those corresponding values to be assigned to variables that I can then use to make the rest of the calculations. I'm having trouble figuring out how to search through the tables based on the input, and I'm not sure how to make it work if, for instance, the calculated average temperature is a value between those given in the table. I'm guessing I'd also need to make the code do linear interpolation between the table values in that case. Any help is greatly appreciated! :)
4 Comments
dpb
on 4 Mar 2019
Is it? :) Maybe you can make the Excel file, but would it have been any harder to build a text data file to read or even just the arrays directly in the ML editor?
Are these data constant or do they also change?
What are the data themselves in format?
Give us some help here; attach a small(ish) subset of what it is you have, then show us what you would want to get from that.
It's always MUCH easier to work from specifics than to try to hypothesize from generalities...
Accepted Answer
Guillaume
on 5 Mar 2019
There's certainly no need for excel, if all you're using it for is passing data to matlab. Using text files would be simpler and faster. If you're sufficiently familiar with matlab, you could even bypass that step and ask matlab to fetch the data directly from whatever website you're using.
As it is, using a text file of the form you show (It's always better to attach an actual file, so that we can see potential issues such as file encoding, formatting, or line ending), it's very easy to import and interpolate in matlab:
%data import
viscosityVStemp = readtable('C:\somewhere\somefile.txt'); %should figure out the format of your file on its own. If not, attach an actual file
viscosityVStemp.Properties.VariableNames = {'Temperature', 'Viscosity'}; %optional: give a good name to the columns, readtable may already have named the columns appropriately
%interpolation
temperature = 23.7; %obtained however you want. Can be an array of temperatures
viscosity = interp1(viscosityVstemp.Temperature, viscosityVstemp.Viscosity, temperature); %array of viscosities the same size as temperature
All done.
2 Comments
Sharjeel Ashraf
on 11 Apr 2020
Doesn't work for me? The interp1 command gives an error. Can you please help?
Guillaume
on 14 Apr 2020
Can you please help?
Not without any more details about the problem such as:
- the actual code you're using
- the input file your using
- the full text of the error message
More Answers (0)
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!