I want to make a function with save option.

I have the following code in my script (this code looks for the file in directory and save with filename#, numbering sequentially).
%To save several files with same name and numbering each file
mylist=dir('file*.dat');%see in the current folder if the file with same name exists
number=length(mylist);%number of files with name 'file'
filename=sprintf('file_%d.dat',number+1);%name the file with number
save(filename,'LP','-ascii')%save the file in .dat format
I want to make this a function so that I can use it in several places by just calling the function. I did like following, but not working!
function [ output_args ] = saveseq( filename, variable )
mylist=dir('filename*.dat');%see in the current folder if the file with same name exists
number=length(mylist);%number of files with name 'filename'
filename=sprintf('filename_%d.dat',number+1);%name the file with number
save(filename,'variable','-ascii')%save the file in .dat format
end
it is saying 'name' and 'variable' are unused!.. how can I feed 'filename' and 'variable' into the code so that it can be used as a function?

 採用された回答

Mahdi
Mahdi 2014 年 5 月 27 日

0 投票

The problem is in the way you're handling the variables. Basically, the inputs to your function in this case are taken as variables, but you're never using the variables inside the function (because of the way you've defined it), basically:
To call the function, use
savesq('name1', 'variable1')
then, inside the function, use
string1=sprintf('_%d.dat', number+1)
filename1=strcat(filename,string1)
save(filename1, variable, '-ascii')

3 件のコメント

aneps
aneps 2014 年 5 月 28 日
編集済み: aneps 2014 年 5 月 28 日
Yes it works thank you... But there is another problem that this time inside the function
mylist=dir('name1*.dat')
is not doing the job! it is not really finding the files with name* (files with name and ending with numbers) in the directory!
aneps
aneps 2014 年 5 月 28 日
Oh.... thank you... I solved it... I just modified as follows
name='Test_1s';
findfile=strcat(name,'*.dat');
mylist=dir(findfile);
Then as normally as you have suggested
number=length(mylist);
string1=sprintf('_%d.dat', number+1)
filename1=strcat(filename,string1)
save(filename1, variable, '-ascii')
This works very well.... thank you again... I think I am improving each day :D
Mahdi
Mahdi 2014 年 5 月 28 日
No problem :)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeManage Products についてさらに検索

タグ

タグが未入力です。

質問済み:

2014 年 5 月 27 日

コメント済み:

2014 年 5 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by