How can I pass the filename as a coefficient in function ?

I would like to use a excel filename as a coefficient as below :
function [ic, is] = Indexing(filename) % such as a.xlsx
[num, txt, raw] = xlsread(filename);
[rownum, colnum] = size(raw);
but, I met the error message which is "Reference to a cleared variable filename."
Error in Indexing (line ) [num, txt, raw] = xlsread(filename);
How can I acheive this in Matlab.
Thank you.

Answers (1)

You have to assign your ‘filename’ variable the name of the spreadsheet you want your ‘Indexing’ function to read:
filename = 'MySpreadsheet.xls';
[ic, is] = Indexing(filename);
This assumes the file is in your MATLAB search path. If it is not, you also have to supply the full path to the file, as well as the name of the file.

2 Comments

Thank you for your assistance. Is there any method to use filenams as a function coefficient directly.
I would like to use a filename as a coefficient in xlsread() inside Indexing() when I call the Indexing() with filename.
Thank you.
I am not sure I understand. If you want xlsread to actually read a file, you have to tell it what file you want it to read.
In your code, ‘filename’ is a variable, so it has to have something assigned to it in order for you to use it in your code. That assignment should be a string with the name of the file you want to use, as in my example.

Sign in to comment.

Products

Asked:

on 3 Apr 2015

Commented:

on 3 Apr 2015

Community Treasure Hunt

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

Start Hunting!