renaming variable's name while working on my script, in matlab - matlab

Working on my script in Matlab, I want to rename a variable, but only the upcoming instances of it the current script.
I am familiar with Matlab rename option when pressing Shift+Enter.
But this changes all the instances of my 'new' variable in the whole script.
I want to change only the proceeding instances.
How could this be done? I also didn't find anything helpful in the Find&Replace window.

Copy the remaining portion of your code into a new .m file
Find and replace the variable you want to change.
Copy and paste it back into your original .m file.

Related

How to save a session on Matlab

I was trying to save my Matlab variables and environment into a session so that next time I can directly load them without repeating the codes. I know how to save the Workspace variables but their way of saving a session doesn't work. It's said to be File -> Save to a session. But I couldn't find the button "File".
Any thoughts?
OK Actually I figured out an alternative solution. I realized that I could save all the Workspace variables. But my variables are too huge. So I ended up saving all my commands in a .m script file.
So next time when I want to resume my work, I just type the name of that script file to run all the useful commands to create the working variables and set up the script paths I need.

MATLAB: Track all possible variables

I Need to understand a very Long matlab file.
It is very tedious to put Breakpoints everywhere.
I wondered if there is an Option to track every possible variable at once ?
Something that would maybe store each variale in a text sheet and diaplays how they Change througout the code...
There are many many variables.
What I want to do is to create a scribt, where I can Input a list of variable names. The script then tracks those variable names within the program and exports them every time they Change.
Input: List of variables and programm Name (other script)
Content: Tracks variable in the Programm
Output: Table with tracked variables
Name_variable_1 |Value at line...|Value at line...|Value at line...
Name_variable_2 |Value at line...|Value at line...|Value at line...
.
.
.
Thanks.
You could maybe save the workspace periodically and then make a seperate MATLAB script that graphs the changes of the variables or writes out a text file for it?
I assume you're familiar with MATLAB, but just to be safe:
http://de.mathworks.com/help/matlab/ref/save.html
Just append the variable's contents to a text file or save the entire workspace and parse it later.

Renaming a gui figure and .m file

I work in a team which shares a variety of Matlab scripts. One of my co-workers built a GUI which he named MT_v1.fig with a corresponding script MT_v1.m.
Now, I have made some edits to both the script and the figure (added a few buttons, etc).
What I want do now sounds like it should be easy: I want to rename the figure and the .m file such that I now have:
MT_v2.fig and MT_v2.m
However, when I try to do this it gives me a whole host of errors because all the callbacks in MT_v2.fig are still referencing the MT_v1.m file. I want to save the two separate version names so that people on the team can easily differentiate the two versions.
Is there an easy way to quickly update all the callbacks in MT_v2.fig so that they reference the MT_v2.m script?
I had the same issue and needed to do the following:
1- Before applying any changes to the "new" GUI/.fig and .m files, save the .fig file under the new name. The corresponding .m file will be generated.
2- Apply your changes to the .fig and/or .m files only once the new versions have been saved.
3- You can then save normally both files under the new name and everything should be fine.
That worked for me hope it does for you!

Save filename.m deleted my origonal filename.m file and replaced it with a new file. How to get the old back?

I tried to save my file using the command window in Matlab. Unfortunately it replaced my file with a new file. And now I am unable to get it back.
It's probably easy, but I am new to using the command window in Matlab.
You are out of luck on this one. Saving to a file will irretrievably overwrite any existing file with that name unless you specify otherwise with the -append option. In the future, if you have a data set that is important because it is either not reproducable or because it takes a long time to generate it, I would recommend either backing it up or saving it with a timestamp. This is one example:
function save_t(name,varargin)
save(sprintf('%s-%d',name,time),clock*[1e8 1e6 1e4 1e2 1 0].',varargin{:});
end
Save this to a file in you matlab path named "save_t.m" and then you can simply call it just like you would call the save function, but now it will add on a timestamp.
save_t filename
This will help to ensure that you don't accidentally overwrite an existing file.

automatically add path in a MATLAB script

I have several MATLAB scripts to share with my colleagues. I have put these scripts under a specified directory, e.g., /home/sharefiles
Under the MATLAB command prompt, the users can use these scripts by typing
addpath /home/sharefiles
Is there a way to automatically add this path in my matlab script, and save users the efforts of invoking addpath /home/sharefiles each time.
Sure, just add the addpath to your script.
addpath('/home/sharefiles')
If you want to recursively add subdirectories, use the genpath function:
addpath(genpath('/home/sharefiles')
Adding files to the path or one of the slower operations in Matlab, so you probably don't want to put the addpath call in the inner loop of an operation. You can also test to see if you need to add the path first.
if ~exist('some_file_from_your_tools.m','file')
addpath('/home/sharefiles')
end
Or, more directly
if isempty(strfind(path,'/home/sharefiles;'))
addpath('/home/sharefiles')
end
You could add the code posted by Pursuit to your startup.m file so that MATLAB adds it to the path automaticlly upon startup. Or, take a look at the savepath function. Lastly,
So when you Use the GUI to set path, the paths get added in the default start directory of Matlab in the pathdef.m file present there. Hence if you are running your code from any other directory either you would have to copy over this file or create a script in the startup folder. Hope this helps!!