How can I find the file extension of the currently running code? - matlab

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

Related

Open and run a .m in matlab into an another .m file (from another directory)

I have a question about a code I am creating.
I have a code in a .m file, let's name it as "first.m".
But in the "first.m" i would like to write a command in this script in which I would like to run and execute an another .m file, let's name it "second.m" file, which is in a different directory.
I mean I would like to use a subroutine, but the main problem is that I do not have understand how can I use subroutines in MATLAB.
Could anyone help me to make it?
try coding using the run function.
It is important that Matlabs knows where to find that function. You can use the function addpath
Also, you can actually copy the full path in that command. Like this:
run('C:\Users\user\Desktop\second.m')
Make sure you write the correct path that contains second.m. If the code is in the same path as first.m or you have already added the path you can use:
run('second.m')
And if they are in a subfolder of the actual folder where first.m is, you can use the first example or:
run('subfolder\second.m')

How to read a file during simulation in Specman

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>")

Save workspace of unknown workspace in Matlab

Is it possible to save workspace variables from a function that I am calling and cannot explicitly edit without file I/O?
I know I can use the save function to save all of the variable names in a workspace, but what if I wanted to save the workspace variables from a function that I am calling, like a built in function (mean, sum, etc).
I would like to save all of the variables from a function's workspace before it returns back to the function I am writing, and I would like to do it without opening the file each time and adding an extra line of code; is this possible?
In case anyone is interested:
I have yet to find a solution to the exact question I asked, but I found a solution that works well enough with a little extra file tracking.
Using the function onCleanup, you can specify that all the variables be saved right before the function returns to the caller. Using this and a little file parsing, you can open the code in question as a simple text file, and insert the onCleanup code anywhere in the file (easier than inserting save as the last line). Then, you can run the code and track the new .mat file using the previous file name or any naming method you choose.
That will enable you to save all the variables in a workspace just before the function exits, but it does require file parsing, see simple example below:
readFile = fopen('filename.m');
writeFile = fopen(['filename_new.m']);
%Ignore the first line (hopefully the function header, may need extra parsing if not)
functionHeader = fgets(readFile);
%Print the function header
fprintf(writeFile,functionHeader);
%Print the clean-up code
%NOTE: This can go anywhere in the file
fprintf(writeFile,sprintf('onCleanup(#()save(''%s.mat''))\n',filename)));
nextLine = fgets(readFile);
while ischar(nextLine)
fprintf(writeFile,nextLine);
nextLine = fgets(readFile);
end
With the above, a new file is created (filename_new.m) which needs to be run, and will create a mat file (filename.mat) with all of the workspace variables in it.
eval(newFileName(1:end-2));
Now, by tracking the .mat file, you can do whatever is necessary after this point. For my purposes, I was interested in the memory used by the said function, which is available by accessing the mat object of the .mat file.
matObj = matfile('filename.mat');
stats = whos(matObj);
fileSize = sum([stats.bytes]);
Try the "save" function.
Add this line in your called function:
save('filename')
Here is my sample code:
a=10; b=6;
c=addition(a,b);
And the function is defined as:
function [out]=addition(a,b)
out=a+b;
temp1=a;
temp2=b;
temp3=a-b;
save('C:\data.mat');
end

How to make matlab see functions defined in .m files?

I'm a total newbie to MATLAB but I have to write some code in it. I've had problems with making MATLAB see functions I've defined in external .m files. This is what I've done: I've created a file named, say, foo.m in my home dir with the following contents:
function [y] = foo(x)
% description
y = x + 1
When I run matlab (my home dir is matlab's workdir) it does not see foo function - it replies with standard ??? Undefined function or variable 'foo' message. BUT help foo or which foo return correct data printing help text and pointing on foo.m file respectively.
I must be missing something but I have no idea what it is. This is getting very annoying.
Oh, after several trial and error attempts I've managed to call that function. Unfortunately I can't remember the sequence of steps I've performed. Moreover after restarting matlab it returns to its usual 'Undefined function or variable' response.
I have 7.11.0.584 matlab running on linux.
MATLAB needs to be told which directories to search over to access those m-files. Clearly it cannot be left to search over your entire disk drives. The MATLAB search path is a list of directories that will be searched in specific order to find your functions.
help addpath
help pathtool
You should never put those files anywhere in the official MATLAB toolbox directories. Choose an entirely separate directory.
Finally, be careful not to name your own functions to match the names of existing MATLAB functions. Otherwise, your very next question here will be why your code does not work properly. This is a common cause of strange and confusing bugs.
It seems you're having some trouble with addpath. Try opening the file in the matlab editor and adding a break point in the file. If the file is not on Matlab's path, matlab should ask if you want to change directory or add the file to the path, choose add to the path.
If this doesn't work, try changing the current working directory (displayed in the main window) to the same location as the m file and calling the function. If this doesn't work you're either getting the name wrong ar there's possibly something wrong with your installation.
Occasionally matlab has problems if it does not have write permission to the directory the file's in, so check that too, i.e. make sure admin rights aren't required for the directory or m file.
Oh, and try:
clear functions
to reload all functions into memory.
The function needs to be in MATLAB's path. Use pathtool to tell MATLAB where to find your function. Note that if you name a function the same name as an existing function, MATLAB will use whichever function it finds first according to the order that the paths are listed as you see them in pathtool.
Although coming late but I hope it will help someone.
If in the folder where the function you are calling is residing, there is any other function with the same name as one of the functions from MATLAB toolboxes, then Matlab will not recognize its license and therefore will disable the whole folder from execution, no matter it is properly added to the path. The help will display though.
In order to check it, type:
which name_of_func.m
and you will get the path with "%Has no license available" message.
If it is your own function, you should not get this message but only the path.
Therefore, find the function in this folder which has the same name as a MATLAB toolbox functions, and rename it. I will solve the problem :).
Best Regards
Wajahat

storing .m files with names equal to the function? scenario if file name is different then?

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.