Mohammad Algassim
Mohammad Algassim

Reputation: 15

how to read from excel to matlab and use the information to find some variables

I have an excel file that contains information about conductors. eg:

type     r        GMR      R
-------- -------- -------- -------- 
b        0.773    0.604    0.238    
c        0.815    0.6614   0.235    
d        0.864    0.698    0.209

I want to write a Matlab program to read these values then display a message 'type of conductor? '

then takes the values from the desired row and uses it to find some values. eg:

If b was chosen i want to find x=ln(r)=ln(.773)  and y=GMR^2 ...
if c was chosen x=ln(0.815)

I know how to write a program that can read form a single row or column but not a whole sheet.

Upvotes: 1

Views: 26209

Answers (1)

Jacob
Jacob

Reputation: 34601

Use xlsread

[num,txt,raw] = xlsread(filename) reads data from the first worksheet in the Microsoft Excel spreadsheet file named filename and returns the numeric data in array num. Optionally, returns the text fields in cell array txt, and the unprocessed data (numbers and text) in cell array raw.

[num,txt,raw] = xlsread(filename,sheet) reads the specified worksheet.

So here's an example program which reads foo.xls into the numeric matrix D and the text info txt.

%# Read the XLS file
[D txt] = xlsread('foo.xls');
%# Assume the columns are fixed (if not see note 3)
r_col = 1; gmr_col = 2;
%# Get the type of conductor
cond = inputdlg('type of conductor?');
%# Search for the right row
index = strcmp(txt(2:end,1),cond);
%# Compute the data
x = log(D(index,r_col));
y = D(index,gmr_col).^2;

Note:

  1. If you don't want a dialog box, replace inputdlg with input
  2. If there are multiple rows with the same type value, then x and y will be vectors
  3. I'm assuming that r and GMR are columns 1 and 2 respectively. If you don't know this, you can find out by using strcmp on the first row of txt. I.e.,

    r_col   = strcmp(txt(1,:),'r');
    gmr_col = strcmp(txt(1,:),'GMR');
    

Upvotes: 4

Related Questions