yankeefan11
yankeefan11

Reputation: 485

Creating functions in MatLab through excel sheets

This is really simple, and for some reason I am having a ton of difficulty doing this. Suppose I have an excel file: "Data.xls" I can do:

a = xlsread('Data.xls','Sheet1','A1:B10');

And i get the data I want. However, I want to write a function to do this:

`function [ data ] = ReadData( fileID,Sheet,Lines )

data = xlsread('fileID','Sheet','Lines');

end

And when I run ReadData('Data.xls','Sheet1','A1:B10')

I get

??? Attempt to reference field of non-structure array.

So I am wondering what is the proper way to do this in a function?

`

Upvotes: 0

Views: 37

Answers (1)

nkjt
nkjt

Reputation: 7817

I'm not sure why you're getting that particular error. However, when you do this:

fileID = 'Data.xls';
Sheet = 'Sheet1';
Lines = 'A1:B10';
data = xlsread(fileID,Sheet,Lines);

Then you are passing those variables into the function, and you shouldn't surround them with ''.

fileID = the string variable, 'Data.xls'

'fileID' = a string which contains the text 'fileID', and has no connection to the file you're trying to open.

Upvotes: 1

Related Questions