Refactoring variables and function names in MATLAB? - matlab

In eclipse you can refactor all variables and function names by hitting CTRL+SHIFT+R which is great. I am editing someone else's matlab script and would like to change a lot of their varaibles and I was wondering if there is something similar in matlab?

Matlab editor in 2012a allows you to press shift + enter to change all of the common variables in a script or function when you start editing one of them.
Otherwise I would suggest find and replace.

Related

detect whether .m file is matlab or mathematica

I have been editing Matlab script in Vim for some time now. Recently I started taking an interest in moving my Mathematica work to plain text files so they can be managed using git and edited using Vim. Unfortunately Mathematica also uses the .m extension for its package files and is therefore not easily distinguished from Matlab scripts. Since I would like my editor to do the work for me I was wondering if anyone has come up with an idea for identifying both based on the contents of the file. I would be fine with something that works in most cases, but am reluctant to use a solution that requires alterations in the scripts such as adding a comment.
A ".mat" or ".m" file created with MATLAB's save() function will always start with the plain text identifier "MATLAB". So, using MATLAB syntax, you might do this:
% Set up a workspace variable and save to file
tmp = 1:10;
save('test.m');
% Open the file, read the first line and close again
fid=fopen('test.m');
firstline=fgetl(fid);
fclose(fid);
% Branch depending on file format
if ((numel(firstline) >= 6) && strcmp(firstline(1:6), 'MATLAB'))
disp('This may well be a MATLAB file.');
else
disp('This is probably not a MATLAB file.');
end

How do I convert Mathematica code into Matlab?

I'm just starting to learn MatLab.
This is what I'd like to convert into MatLab code:
http://postimg.org/image/jqdvcrbod/
Since a lot of my variables are functions or other variables, does that mean that when I write them as functions in MatLab I have to save each of the functions as a separate file? Is there any other way? I don't want to end up with a million separate files, but as of right now, if I write more then one function in the editor, it starts getting confused and doesn't recognize the second function.
Also, is there a way to use actual symbols (like the square root symbol instead of writing "sqrt()") like in Mathematica? I feel like long equations (like the last one) are easier on the eye that way, but that's purely aesthetics.
Is there a way to have MatLab output unassigned variables? Like in Mathematica, if I have y(x) = 2x, if i don't put anything for x, it just outputs 2x.
One quick way to do is use a package in Mathematica called ToMatlab (I attached the download link). It is able to convert Mathematica symbol syntax to Matlab .m file.
Hope this would help.

MATLAB: F1 help function in Editor refers to a wrong function

I am using MATLAB 2014b.
If I write a line like
A = regexp(strcat('some string'),expression);
Placing the cursor on the first function (here regexp) lets me get the content help of it by pressing F1.
If I now move the cursor to strcat I also get the content help of regexp. This is just an example; the same problem happens for every function.
In MATLAB 2012b, I get the correct behavior. How can I make MATLAB 2014b act the same way?
Note: If I highlight the function name and press F1, I'll get the right content help. But this is just a workaround.
I don't have MATLAB 2012b to test on right now and I'm running 2015a, but I feel it might be an issue with your test case. Try putting the cursor within the brackets that the function is called with. Let me know if it works in 2014b!
regexp help:
strcat help:

Use both GUI and script at the same time

I have a .m file (script) that is controlling a real-time robot.
What i do within this file is:
1- find a trajectory
2-infinite loop:
read from robot
update robot
plot some stuff (basically I'm drawing a new point in each iteration that represents the position of the robot in a previously opened map, it's updating the map)
end of loop
What I want to do is to create a GUI that allows me to make the plots and see some values that the robot returns at the same time, in real time.
From what I read, MATLAB can't run both a script and a GUI at the same time.. I can make it plot in real time in the GUI but I can't seem to be able to update the values returned by the robot in text boxes in GUI..
Do I have to put it all in the same file or is there a way for the GUI and the script to work in separate files?
Thank you in advance!
MATLAB has not trouble running both. I don't know where you read that but it's not true; MATLAB is not the best tool to solve this problem, but it can do it.
First, I'm going to frame your problem in code, to make it easier to solve. Your question is vague and general, so my response has to be be general as well. I'm making some assumption about your function structure, but it really should look a lot like this:
endflag = 0;
while ~endflag
robotData = getRobotData(robotHandel);
derivedData = doStuffWithData(robotData);
updateRobot(derivedData);
showData(robotData, derivedData)
endflag = checkEndFlag(robotData, derivedData)
end
So, your problem is the showData functionality. What it should do, is determine which values need to be displayed from it's inputs, and pass those to your GUI. Like so:
function showData(robotData, derivedData)
guiInputData = dataParser(robotData, derivedData)
YourGUIFunctionName(guiInputData)
end
The GUI function should then build itself using those inputs. Any GUI function working that way, will do what you want it to. If you want a more specific solution, you need to give me more specific information about your problem. Good luck, I hope this helps.

How to find call an external function from a GUI in MATLAB?

I have an external function (myfun.m) which will generate 4 images. I want to use a textbox in a GUI to give the function the input parameters (e.g. the name of the original picture), but I have no clue how I can run myfun.m in my GUI. PLease help
First, I would recommend having a different textbox for each input to your function. For example if your function asks for 4 inputs, I would have four textboxes. This will avoid pain on the user's end to format their text input a certain way, as well as pain on your end on parsing a long text string into several inputs.
Second, if your function is within MATLAB's path, then you can call that directly from your GUI no problem just as you would any of MATLAB's built-in functions. You probably want to make a push-button that has a callback to execute that functions.
The way the callback for this function should look is to l