I want to delete some variables that I have declared in command window in matlab, I tried clear all but it didn't work. I typed the following in the command window,
e.g.
a = 10;
str = 'a';
clear all
Matlab doesn't delete them, why?
Is there any function that can do this for me?
1)
If you have redefined function clear (e.g. by a variable or function), you can use the builtin function to execute built-in clear function.
I.e. you can use
builtin('clear','all')
to clear all variables,
respectively
builtin('clear','clear')
to redefine clear to the built-in clear function and then use it normally
clear all
2) If this was not the solution of your problem, could you show us the output of the following code?
a = 10;
str = 'a';
builtin('clear','all')
builtin('who')
To clear all of your variables in matlab you type:
clear
If you want to clear a specific variable, for instance "a" you tpye:
clear a
I suspect your problem is that you have named a variable "all" so when you use clear all you end up only clearing the variable all.
You should be careful about what names you are giving to your variables. You can useexist {variable} to check if the variable you want to assign is already used by a matlab function.
use "Clear" only it will work or u want to remove particular variable the put variable name in last of " Clear Var_name"
Related
I'm getting started with PyDev but am having trouble understanding its code validation.
For example, in a file containing just the code below, PyDev warns "Unused variable i" in the third line, but has no issue with j in the first line.
l1 = ['a' for j in range(10)]
def test():
l2 = ['a' for i in range(10)]
What is triggering PyDev's warning in this example?
(I know to avoid it by adding # #UnusedVariable)
The difference here is that in the first case you're creating global variables and in the second a local variable (global variables created are not reported because they may be used by another module).
You may add an underline (_) in front of the variable (i.e.: _i) to signal that you know it is not used and shouldn't be reported.
Title says it all, I just want to change this
addr:
loc_74E240
That's all.
This is for ROBLOX by the way
To define loc_XXXXXX as a function you should use P. This is used to create a function where your cursor at. In some cases you'll need to undefine the current analysis done by IDA on a specific place and then redefine it as code. You can do it by pressing U and then C. Finally, by using ALT+P you can edit an existing function.
Keep in mind that creating and editing functions might cause with unwanted side-effects, so be careful with that.
For more shortcuts, see the Quick Reference Sheet by Hex-Rays.
Just use p to define the address as a function. Be careful with that because you can ruin the disassembly.
I use dbstop error a lot when working in Matlab. A good portion of the time, a mistake causes errors to be thrown inside of built-in [m-file] functions, which then causes Matlab to stop execution and open the file. However, it's almost never helpful to debug inside of the built-in file, so this ends up disrupting my workflow. Might there be a way to set things up so that Matlab backs out of the built-in file in the debugger (never opening it), leaving me at the function call?
Although I've never found a way to tackle this problem properly, it's fairly easy to hack together a workaround:
Create a script containing something along these lines:
S = dbstack();
file_paths = cellfun(#which, {S.file}, 'UniformOutput', false);
builtins = ~cellfun('isempty', strfind(file_paths, matlabroot()));
stack_depth = find(~builtins, 1, 'first');
for ii = 1:stack_depth-1
dbup(); end
Save it somewhere that makes sense to you, and place a shortcut to it in the MATLAB toolbar.
Then, whenever this problem occurs, you just click on your little shortcut, which will automatically take you to the first non-builtin function in the debug stack.
Based on Rody's answer and feedback from Mathworks, this is the closest you can get at this point (R2016b):
S = dbstack('-completenames');
builtins = ~cellfun('isempty', strfind({S(:).file}, matlabroot()));
stack_depth = find(~builtins, 1, 'first');
hDocument = matlab.desktop.editor.findOpenDocument(S(1).file);
matlab.desktop.editor.openAndGoToLine(S(stack_depth).file,S(stack_depth).line);
hDocument.close();
if stack_depth == 2
dbup();
end
This shortcut will:
Open up the closest user function to the correct line.
Close the builtin function that opened when the error was thrown.
If the error happened only one level away from a user function, switch to that workspace.
The problem is that dbup() only works once - after the call, execution in the script stops. There's no function to switch to an arbitrary place in the stack.
As stated in MATLAB's FAQ,
1.3.3.1. Why, when I edit a function file in MATLAB, is the change not seen by MATLAB until everything is cleared or MATLAB is restarted?
When you write an M-file in MATLAB, you can either write a script or a
function. The difference is that a script is read from the disk and
parsed line by line each time it is called. A function is loaded into
RAM for execution. Because it is loaded into RAM, when you edit a
function, that change is not loaded into RAM until a call to the new
function is made.
To get MATLAB to recognize your edited function, type
clear functions to clear all functions, or
clear <function name> to clear just your function out of RAM.
This is a major pain when I'm developing a function & editing it repeatedly (I use an external editor most of the time). I was thinking of putting in a final line, at least during debug, like
clear(myfunc)
but I'm concerned about unwanted side effects. Does anyone know if there are any?
Further, I'd rather have a way to configure MATLAB so it doesn't automatically store called functions in RAM once the top-level function (i.e. the one called from the console) terminates. Is that even possible?
EDIT: I should mention that MATLAB's behavior is inconsistent. Sometimes my edits take effect once I save the m-file, other times they don't even if I'm editing with the MATLAB IDE editor window.
In all honesty MATLAB has a very powerful editor so you really should
be using it. It will make you life easier. (just an opinion)
Unless you are running the code by stepping through it no change to code will be run until the code is re-run (preferably with a cleared workspace).
clear(myfunc) will clear the variables created by that function in upto that point. You can add at the end of any function or script clear variableA variableB for all variables you want cleared at the end. This will give you control to only clear what you want. Once variables are cleared the only effect will be that if those variables are called again later in the code an error will occur since they no longer exist.
If your simply testing that particular function and want to save time by not calling inputs each time and want to clear the workspace and command window at the same time. You can add the following code just beneath the function definition.
If you leave the following code and call the function from another function or script, the code will be ignored as long as the inputs are available to it externally. Anything you add to the if statement will only occur when you call the function with no inputs doc nargin. You could add it to the top level function and you would simply press the run button or f5 without having to type in the command window to run the code.
function [ output1, output2 ] = blah( input1, input2 )
if nargin == 0
clc
clear all
%above two lines will clear the workspace and command window when you run
%the function
%define function inputs
%(optionally add the following to behave as if you inserted a breakpoint
%in the location just before the error occurred (great for debugging)
db stop if error
end
*you can also add a short-cut to snippets of code up in the top right of the MATLAB interface to clear the workspace etc when they are clicked.
Hackish* idea for this:
I'd rather have a way to configure MATLAB so it doesn't automatically store called functions in RAM once the top-level function (i.e. the one called from the console) terminates. Is that even possible?
* This is the sort of thing I might use, while advising others on why it is a bad idea, also probably uses undocumented (thus unreliable) things.
Specifically if your using an external editor I guess your going to have to click on the command window to run the function...
commandWindowHandle = ...
handle(com.mathworks.mde.desk.MLDesktop.getInstance.getClient('Command Window'...
.getComponent(0).getComponent(0).getComponent(0),'CallbackProperties');
commandWindowHandle.MouseClickedCallback = 'clear functions'
It doesn't clear the function definitions on exiting a function rather on clicking on the command window
note: the callback will not fire if a function is currently running
For a potentially more reliable but more wasteful version:
commandWindowHandle.KeyPressedCallback= 'clear functions'
will help* ensure definitions are clear, but is wasteful as every keystroke will run 'clear functions'... although after one keystroke this will be quicker as there will be no functions in memory!
* No guarantee I know there are ways in which this could fail... having at least 1 keystroke in the command window after the previous function call has finished would be advisable!
To disable these set the callback to empty string ''
commandWindowHandle.MouseClickedCallback = ''
commandWindowHandle.KeyPressedCallback= ''
Is there any way to make MATLAB run a certain chunk of code every time you try to run a script? For instance, I would like MATLAB to run
sprintf('Here we go...')
as soon as I hit the Run button and then move on to execute my script, so if my script were
i = 1;
i = i * i;
display(i)
I would get
ans =
Here we go...
i =
1
P.S. I would appreciate it if the people with higher reputation please corrected the title of my question for it to better reflect the content.
as soon as I hit the Run button
I am assuming you are talking about the run button in the editor. In R2012a there was a feature called "Run Configuration". A run configuration was linked to a specific script and included code to be executed prior to the script being run. There does not appear to be a global setting to be used on all function. This feature appears to have been silently removed in R2012b.
In R2013b you can chose to run a different script. Presumably you could hack the editor to get the current file and use the custom run script to run your preamble and then the current editor file. This seems like a lot of work for not much return ...
You could create a file called myrun.m
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
jEditor = desktop.getGroupContainer('Editor').getTopLevelAncestor;
title = jEditor.getTitle;
currentFilename = char(title.replaceFirst('Editor - ',''));
fprintf('Here we go...');
run(currentFilename);
and this in the editor under run Run: type code to run type myrun. One you do this once it will remember your preferences and you can then run you code via myrun with F5. It will remember your preferences across restarts.
The way to do this would be to have a preamble.m and doThis.m. In preamble.m you'd have this:
sprintf('Here we go...')
and then in doThis.m, you'd have
preamble
i = 1;
i = i * i;
display(i)
The only trick to making this work is to have them both on the path, or in the same directory.
Not sure if I got what you want, but you can divide Your m file into Code Sections. For example:
%% Section 1
sprintf('Here we go...')
%% Section 2
i = 1;
i = i * i;
display(i)
The %% is a section break. Place your cursor in the relevant section, and on the Editor tab, in the Run section, click Run Section. (or press Ctrl+Enter)
see here for more info.
If you only want this for one (or a few scripts) either add the command in the script, or make a wrapper function/shortcut.
If you want this for many scripts without input, you can create a generic wrapper:
Suppose you want to run things like myFun(a,b,c) then create a wrapper that you can call like this:
myWrapper('myFun(a,b,c)')
Then you can first call your display command and then use eval on the input of myWrapper. Note that this becomes cumbersome if your function call is multiline or contains quotes.
If these solutions can't help, you probably need to ask yourself why you are trying to do this and whether there is a better solution for the underlying problem.