I want to use script file as function because somehow, I need to define methods in script file and it is not possible in script file. However, when I defines script as function and execute it then I lose work space and I can not do further analysis of variables. I do not want to set breakpoints in code. Is there a way to keep work space alive for further analysis.
At the end of the function:
save filename;
evalin('caller','load filename');
I just got an evil solution:
%get all the var-names of the fcn-workspace
myVars=who('*')
for var=myVars'
assignin('base',var{:},eval(var{:}))
end
..this will work, but it is eval :)
Related
I have many small functions in matlab, is it possible to put them all in one file so my work will look clearer?
i tried writing a small script and then adding the functions but is didn't work
any idea on how do do it?
It is not possible to have several functions in one file, because the function is accessed via the file name. That is why a function has to have the same name as the file name.
If you only access the "small" functions inside one other function, then you can put the small functions in the file of the other function, but then they are only accessible to this one function. This is called local functions in MATLAB. For example you have a file test.m with:
function x=test(y,z)
x = add(y,z)
end
function a=add(b,c)
a = b + c;
end
You can then only use add inside test, but you can use test just as usual.
What I usually do is put functions in subfolders. This helps you keep your path clean without any limitations. This also allows you to capsule your software better. The only thing you have to do is add the folder to your path with
addpath('subfolder');
If you have a function file, you can add other functions in that file.
If you have a script, you cannot add functions to it.
Note that if you put a function in a file, you cannot access the functions directly from outside your 'main' function scope.
In general I would recommend the use of folders, or proper file names to organize your functions, not stacking many of them in one file.
Extra
If your functions are really small and trivial, you could write a script with the declaration of anonymous functions for easy reuse. However this is probably not what you want.
I would like to update variable editor at a predefined interval during a running script without having to use a break. Is this possible? I have a sim that runs for hours, I would like to be able to come back every hour and grab some values off a matrix in variable editor to play with in excel without interrupting running script. Any ideas would be greatly appreciated.
I think using assignin to copy your matrix to the base workspace should do exactly what you want. You'd need to manually reopen the variable in the editor to reflect the new data if it's changed.
If you wanted to get fancier, I imagine you could script evalin and openvar to do it for you, but I no longer have real Matlab to test with and Octave's fledgling GUI isn't there yet.
I need to create a matlab mfile that will run another matlab file with default values given in a txt file. It's ment to be useful for testing programs, so that user may specify values in a txt files and instead of inputing values every time he starts the program, my script will give the program default values and user will only see the result.
My idea is to load tested file into a variable, change 'variable=input('...');' for variable = default_variable;, save it to tmp file, execute, and than delete tmp file. Is this going to do the job?
I have only two problems:
1) How to eliminate the problem of duplicated variable names - i mean this must work for all scripts, i don't know the names of variables used in tested script.
2) As I wrote before - is this going to work fine? Or maybe I missed a easier way to do it - for example maybe I don't have to create a tmp file?
I really need your help!
Thanks in advance!
If the person who has to edit the default values has access to Matlab, I would recommend saveing the values in a mat file and loading them when needed. Otherwise you could just write a smalls cript that contains the assignment to certain variables, but make sure to keep this small. For example:
maxRuns = 100;
clusters = 12;
So much for setting up the defaults. Regarding the process my main advice is to wrap the thing that you want to test into a function. This way variables used in the code to call the 'script' will not interfere as a function gets its own separate workspace. Check doc function if you are not familiar with them.
I have a matlab script lets say image_process.m and the function image_process accepts one argument which is the picture.
I want to define a path for the images folder so now I can call image_process path image_name.
I know I can do image_process path/image_name but I need to add the extra argument to my function.
It is quite easy, just use the fullfile function to combine
function foo(imageName,cDir)
fullFileName = fullfile(cDir,imagesName)
% Do something with fullFileName
end
If you are using scripts instead of functions, just convert it to functions.
Scripts are bad practice anyway, because they mess with the global workspace, and cause havoc.
Any script can be converted to function.
Call your function like this:
foo('peppers.png','C:\MySpecialDir')
A function I'm using has display() in it (or other things that display messages on command window), so it outputs a lot of things (x 1200) on command line when I run my code, which makes things hard to track and observe.
Is there a way to suppress the output of this specific function? Ending the statement with semicolon obviously doesn't help.
You might try wrapping the call to the function in an evalc:
evalc('out = func(arg1, arg2);');
The easiest way is to just create a dummy function DISP/DISPLAY and place it in a private folder along with your own function:
private/disp.m
function disp(x)
return
end
myFunc.m
function myFunc()
%# ...
disp(1)
end
By placing the disp function inside a private folder, you override the built-in function with the same name, yet this version is only visible to functions in the parent directory, thus maintaining the original functionality in other places.
Make sure that you DON'T add this private folder to your path, just have myFunc.m on the path (Please read the relevant documentations)