Using matlabs save in functions - matlab

Is it possible to use the Matlab save command inside a function to store workspace variables?
Consider following scenario: I've got a bunch of variables in the Matlab workspace and want all that are beginning with "a" and "b" in a .mat file. Of course this works:
save('test.mat','a*','b*')
but i want to have a variable filename. The function i wrote:
function save_with_name(name)
save(name,'a*','b*')
does not work, because save_with_name doesn't see the workspace variables. Is there a solution which i can use?

You need to evaluate save in the base workspace.
function save_with_name(name)
expression = ['save(''', name, ''',''a*'',''b*'')'];
evalin('base',expression);
The double-quotes ('') in the expression are necessary to allow the quote character itself (').
Thus the command you're looking for is: evalin

Related

How to read a variable in set_param SIMULINK function?

I am having a problem reading a value from a file and put it inside Set_param function which will change SIMULINK model parametrs. This is my code where here i get the value of A from a txt file but i want to put A in Set_param.
when the simulink open it shows A not the value of A in the model.
open_system('Transient.slx') %this will open the simulink model
% get a value from txt file and put it in variable A
A= dlmread('C:\xampp\htdocs\RCE\MATLAB\FYP_expirement\SpeedControl\exp_value.txt');
% here when i put the variable A the function does not accept it
set_param('Transient/Gain','Gain','A')
i try A without single quotation also it gives error.
set_param('Transient/Gain','Gain', A)
how i can insert a variable in this function ? or is there any other solution ?
Thank you very much i find a way to make it works. it seems that set_param only accept characters. So after getting the value i should convert it to string like this:
A= dlmread('C:\xampp\htdocs\RCE\MATLAB\FYP_expirement\SpeedControl\exp_value.txt');
s = num2str(A)
set_param('Transient/Gain','Gain', s)
then when i insert s in the function i dont have to use quotation.

clearing many MATLAB functions in one single file

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.

How to define a path in matlab script?

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

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.

How do I emulate 'include' behaviour in MATLAB?

In MATLAB I can define multiple functions in one file, with only the first defined function being visible external to that file. Alternatively, I can put each function in its own file and make them all globally visible through the path. I'm writing a menu driven application, where each menu item runs a different function. Currently, these are all in one big file, which is getting increasingly difficult to navigate. What I'd like to do is put groups of related functions into separate files.
I think I can do something like this by putting all the child functions into a separate directory and then adding the directory to the path in my parent function, but this feels a bit messy and inelegant.
Can anyone make a better suggestion?
Note: I'm most familiar with MATLAB 2006, but I'm in the process of upgrading to MATLAB 2009.
One suggestion, which would avoid having to modify the MATLAB path, is to use a private function directory. For example:
Let's say you have a function called test.m in the directory \MATLAB\temp\ (which is already on the MATLAB path). If there are local functions in test.m that you want to place in their own m-files, and you only want test.m to have access to them, you would first create a subdirectory in \MATLAB\temp\ called private. Then, put the individual local function m-files from test.m in this private subdirectory.
The private subdirectory doesn't need to be added to the MATLAB path (in fact, it shouldn't be added to the path for things to work properly). Only the file test.m and other m-files in the directory immediately above the private subdirectory have access to the functions it contains. Using private functions, you can effectively emulate the behavior of local functions (i.e. limited scope, function overloading, etc.) without having to put all the functions in the same m-file (which can get very big for some applications).
Maybe something like this,
function foobar
addpath C:\Include\ModuleX
%% Script file residing in ModuleX
some_func();
end
Of course, ModuleX will remain in your search path after exiting foobar. If you want to set it to the default path without restarting, then add this line:
path(pathdef)
See ADDPATH for more details.
You can use sub-folders that begin with "+" to separate functions into namespaces.
For example:
Place a function "bar" in the folder "+foo"
function bar()
print('hello world');
This function can be used as:
foo.bar() % prints hello world
More information can be found here:
What is the closest thing MATLAB has to namespaces?