How to read values from a table or Excel File and assign it to a variable ?

21 ビュー (過去 30 日間)
Hello, I am writing a MATLAB code for the design of a Horn Antenna. In the process, the code is supposed to read values from a table (or an excel file). To make it more clear, let us assume I have a table of two variables "a" & "b". The value of "a" is to be entered by the user and based on the value entered, the code is supposed to look up the table/or an excel file for a corresponding value of "b" and assign it to "b". How do I do it ?
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 5 月 1 日
for a corresponding value of "b" and assign it to "b"?
Ramann Mantha
Ramann Mantha 2014 年 5 月 1 日
I'm sorry, I meant to say that depending on the value of "a" entered by the user, the code is supposed to find the corresponding value of "b" from the table ! Hope this clears the confusion !

サインインしてコメントする。

採用された回答

Benjamin Avants
Benjamin Avants 2014 年 5 月 1 日
You'll have to load the whole table into memory and then search column a for an entry that matches the user input. Then you use the index of a to get the value for b.
To load the table into memory, use the MATLAB function xlsread.
data = xlsread('myFile.xls')
There are options to restrict how xlsread opens the file if you need to read specific ranges of cells or cells that include text. Look up the documentation if necessary.
The above function call is assumed to work and the file contains two columns "a" and "b".
Finding your "a" value would be something like this:
index = data(:,1) == a;
and saving your "b" value would work like this:
b = data(index,2);
In this case, if "a" exists more than once in the data, you'll get an array of "b"s associated with those "a"s. You can combine the above lines like this:
b = data(data(:,1) == a,2);
  1 件のコメント
Nikhilesh Gandhre
Nikhilesh Gandhre 2019 年 9 月 13 日
what if there is column c and d?then will this work?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import from MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by