I want to make a function with save option.
5 views (last 30 days)
Show older comments
aneps on 27 May 2014
Commented: Mahdi on 28 May 2014
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
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 on 27 May 2014
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
then, inside the function, use
save(filename1, variable, '-ascii')
More Answers (0)
Find more on Search Path in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!