I need my MATLAB function to modify the input file name (which I am passing as input argument to the function) each time I run it.
For example, if I pass input arguments like this: func_name('file.wav') to my function func_name, then how do I write a code to save a new file named filenew.wav?
While .wav is the extension for sound files.
You can use the command fileparts to seperate the path, file name and extension.
You may also find fullfile command to be useful to create the new file name with path.
Related
MATLAB provides the mfilename function. It returns the name of the file where the function was invoked, but unfortunately, it returns the file name without the extension.
So for example, if we have a file called myfile.m and we call mfilename inside the file, it will return the string 'myfile' but not 'myfile.m'
I also had a look at the fileparts function but it is not useful either because it only parses the string that you provide.
I am developing a piece of code has a different behavior based on the file extension. So for instance, it needs to know whether the extension of the file is .m or .p at run time.
You can check the list of extensions associated with MATLAB here.
How can I do it?
Looking at the docs, it seems like you can get the information you need from the dbstack command, it will take some minor additional processing though.
[ST, I] = dbstack('-completenames', 1)
ST =
file: 'C:\myProject\myfile.m'
name: 'myfile'
line: 2
I need to read a file with input parameters for my test. However I dont want to hardcode the name of the file into the code.
How can I specify the name of the file from the command line for compiled e code?
Is there another way to do it for loaded e code? Why wont this work for compiled code?
The generic solution would be to use the new sn_plus mechanism.
From command line add something like +my_file=filename
From your code you can access the argument with special functions sn_plusargs_exist to check if there is such argument, and read its value with sn_plus_value.
Another solution is passing filename as define from the command line, with -c flag, and inside your code read the file named with that define.
However, it doesn't work with compiled e code, since the defines are already calculated at compiling time.
You can use the sn_plusargs_value() and sn_plusargs_exist() in your code.
Now you can pass your arguments file via command line with no need to re-compile your e code.
Alternatively you can set an environment variable and retrieve its value in the e-code using
var filename := get_symbol("<VAR>")
I have a short question regarding the copyfile function within MATLAB. Basically I want to copy a file from a different user selected directory/file to the current directory (where the function is run from). I am struggling with how to do this.
So far I have:
[jxlfilename,jxlfilepath] = uigetfile({'*.jxl'}, 'Pick a File');
copyfile(????)
I have read the help that MATLAB offers, but I just can't seem to figure it out.
The syntax for copyfile is
copyfile(source,destination);
The function to concatenate paths and filenames is fullfile .
The current directory is selected with .
Together this gives you
[jxlfilename,jxlfilepath] = uigetfile({'*.jxl'}, 'Pick a File');
copyfile(fullfile(jxlfilepath,jxlfilename),'.');
you were almost there, once your selected your file do:
copyfile(strcat(jxlfilepath,jxlfilename))
and if you don't specify a second argument copyfile will copy the file to the current folder and the strcat(jxlfilepath,jxlfilename) will construct the string with path and filename.
Or
copyfile(strcat(jxlfilepath,jxlfilename),'newname.jxl')
if you want to assign a new name to the file.
I want to write the file name in the gui edit text and save edit text as my filename. Than I could be able to save filename as I wish. For instance; My file is an Neural network file which could be save as *.mat file; here is my code
%network_name is my edit text
name = get(handles.network_name,'string');
name = net;
save name
But it doesn't work I can't manage file name from edit text :(
It saves as name that l wrote next to save (name.mat). Thanks for your any answer...
Why name = net;?
That aside, if you want to pass the filename as a variable to the save function, you need to use this syntax instead:
save(name)
save name is the "command form", and save('name') is the corresponding "function form". As you can see you can only give string inputs when using command form, whilst you actually want to pass a variable.
To conclude: if you are passing variables to a function, use function form.
If you want to save particular variables, use:
save(name,'net');
Note that name (which we want to be the string contained in the variable) isn't in quotes and net (the name of the variable we want to save) is.
A warning about this is actually buried in the documentation for save.
Do not use command form when any of the inputs, such as filename,
are variables.
save name
will save all of the variables in your workspace to a *.mat file called 'name'
Also your code is basically overwriting itself, line 2 sets the variable name to be a string, but then line 3 writes some data net to that variable.
I'm a little confused as to exactly what you want but I think you want to save the variable net to a mat file with the name that you read in the string from get(handles.network_name,'string')
If that is what you want to do then its just
save(get(handles.network_name,'string'), net)
If you want to save all the open variabile in the workspace then its just
save(get(handles.network_name,'string'))
Before we begin, we must first look at the syntax for a function in MATLAB.
function y=(argument list)
commands
The code above must be written in a separate m-file! The name of the file should coincide with the name of the function, i.e. .m ?? why what if not
The function syntax is:
function y=functionname(argumentlist)
commands
the functionname and the .m filename should be the same.
Why?
Suppose you want to call that function from another .m file or the matlab command line, it is most logical to call it with the function name. but if you use another filename as the function name, matlab wont find the function. Instead you had to call it with the filename, which would also work, but is unlogic.
So you can say, its a matter of good style.