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)
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
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...
Emilee
Emilee on 5 Mar 2019
Okay so I put the data into notepad (.txt file), some of which I pasted below. The data is constant. I just don't know how to ask ML to open and search through the data for, say, 23.7 degrees C and give me the matching kinematic viscosity and assign it to a variable. I've read some of the Mathworks how-to pages but don't understand them all.
Temperature (C) Kinematic Viscosity (m^2/s*10^-6)
15 14.61
20 15.06
25 15.52
30 15.98
40 16.92

Sign in to comment.

Accepted Answer

Guillaume
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
Guillaume
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

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!