Function input as a string

Hi, I want to input a filename which is a string (filename = 'exceldata.xlsx') to the input of a function. Inside the function I would like to importdata with designated filename. The function is not processing the filename as 'exceldata.xlsx'
Example Code
filename = 'exceldata.xlsx;
function [a b] = readexcel (filename)
A = importdata(filename);

回答 (1 件)

Adam
Adam 2016 年 4 月 11 日

0 投票

If you just give a filename with no path the file must be on your path. Generally you should give a full path to the file to be more robust.
You need to give more information than "is not processing" though. If you have an error message then post it in full, or give full details of whatever the problem is you get if it isn't an error.

3 件のコメント

Jan
Jan 2016 年 4 月 11 日
編集済み: Jan 2016 年 4 月 11 日
Moved from pseudo-answer (please post comments in the section for comments, thanks.)
Adrian wrote:
Hi, thank you for the quick response I have added some more details below
Filename = 'Matlab_Decrement_Data_415V_50H.xlsx';
[UPeaks VPeaks WPeaks] = Decrement_Peak_Detection_Function (Filename,Uthres, Vthres, Wthres, Udelta, Vdelta, Wdelta, UFPP, UFPN, VFPP, VFPN, WFPP, WFPN);
A = importdata(filename); % Load data from file
Error
Error using importdata (line 136) Unable to open file.
Error in Decrement_Peak_Detection_Function (line 24) A = importdata(Filename); % Load data from file (structure)
Error in function_test (line 26) [UPeaks VPeaks WPeaks] = Decrement_Peak_Detection_Function (Filename,Uthres, Vthres, Wthres, Udelta, Vdelta, Wdelta, UFPP, UFPN, VFPP, VFPN, WFPP, WFPN);
The importdata(filename) works fine outside the function
Jan
Jan 2016 年 4 月 11 日
@Adrian: As Adam has mentioned already, the error message means, that the file is not existing in the current folder. So add the folder excplicitly:
Folder = '???'
Filename = 'Matlab_Decrement_Data_415V_50H.xlsx';
File = fullfile(Folder, Filename);
and deliver File instead of Filename.
Adrian
Adrian 2016 年 4 月 12 日
Thank you Jan, the fullfile worked :-)

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

カテゴリ

タグ

質問済み:

2016 年 4 月 11 日

コメント済み:

2016 年 4 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by