How to read a variable in set_param SIMULINK function? - matlab

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.

Related

SPM in matlab: how to call matlab function in Batch Editor

I'm writing a batch using the Batch Editor in SPM8 using matlab. Now I want to include a simple matlab function that I have written myself. I make sure this function is saved in the path of SPM. As input variable the function uses a file from a previous step in the batch, and when I specify the function to be called, it says "Input could not be evaluated".
So now I'm trying to make another easy function work in the SPM batch, for example "sqrt". I put the "Evaluated Input" to 25, the "Type of output variable" to real number, and the "Function to be called" to sqrt. Again there is an error message saying "Input could not be evaluated".
What am I doing wrong here?
Enter the function in single quotes as follows:
'sqrt'
I just tried it and it worked.
Your batchfile should end up looking like this:
matlabbatch{1}.cfg_basicio.run_ops.call_matlab.inputs{1}.evaluated = 25;
matlabbatch{1}.cfg_basicio.run_ops.call_matlab.outputs{1}.strtype.r = true;
matlabbatch{1}.cfg_basicio.run_ops.call_matlab.fun = 'sqrt';

Save workspace of unknown workspace in Matlab

Is it possible to save workspace variables from a function that I am calling and cannot explicitly edit without file I/O?
I know I can use the save function to save all of the variable names in a workspace, but what if I wanted to save the workspace variables from a function that I am calling, like a built in function (mean, sum, etc).
I would like to save all of the variables from a function's workspace before it returns back to the function I am writing, and I would like to do it without opening the file each time and adding an extra line of code; is this possible?
In case anyone is interested:
I have yet to find a solution to the exact question I asked, but I found a solution that works well enough with a little extra file tracking.
Using the function onCleanup, you can specify that all the variables be saved right before the function returns to the caller. Using this and a little file parsing, you can open the code in question as a simple text file, and insert the onCleanup code anywhere in the file (easier than inserting save as the last line). Then, you can run the code and track the new .mat file using the previous file name or any naming method you choose.
That will enable you to save all the variables in a workspace just before the function exits, but it does require file parsing, see simple example below:
readFile = fopen('filename.m');
writeFile = fopen(['filename_new.m']);
%Ignore the first line (hopefully the function header, may need extra parsing if not)
functionHeader = fgets(readFile);
%Print the function header
fprintf(writeFile,functionHeader);
%Print the clean-up code
%NOTE: This can go anywhere in the file
fprintf(writeFile,sprintf('onCleanup(#()save(''%s.mat''))\n',filename)));
nextLine = fgets(readFile);
while ischar(nextLine)
fprintf(writeFile,nextLine);
nextLine = fgets(readFile);
end
With the above, a new file is created (filename_new.m) which needs to be run, and will create a mat file (filename.mat) with all of the workspace variables in it.
eval(newFileName(1:end-2));
Now, by tracking the .mat file, you can do whatever is necessary after this point. For my purposes, I was interested in the memory used by the said function, which is available by accessing the mat object of the .mat file.
matObj = matfile('filename.mat');
stats = whos(matObj);
fileSize = sum([stats.bytes]);
Try the "save" function.
Add this line in your called function:
save('filename')
Here is my sample code:
a=10; b=6;
c=addition(a,b);
And the function is defined as:
function [out]=addition(a,b)
out=a+b;
temp1=a;
temp2=b;
temp3=a-b;
save('C:\data.mat');
end

Matlab Error: "Undefined function or method X for input arguments of type 'double'" With Recursion

I'm trying to make Sierpinski triangles with recursion but I get this error:
??? Undefined function or method 'sierpinski' for input arguments of type 'double'.
I understand that it has to do with Matlab not finding the path for my function, but the weird thing is that it can find my main sierpinski(x,y,n)-function but not the same function that I'm trying to call later in order to get recursion.
My code looks something like this:
function sierpinski(x,y,n)
...
sierpinski(x2,y2,n-1)
end
sierpinski([0,1,0.5],[0,0,1],4)
I would be very grateful if someone could help me with this :)
I cannot reproduce the first error you report. It probably has to do with the file not being on the path. The easiest way to avoid this is to change the working directory to the directory that contains the .m file.
The second error you describe in your comment is due to the fact that you're trying to have a file that is a Matlab function and a Matlab script at the same time. Both have the extension .m, but the first contains a function definition (something that can be called with arguments, has local variables, and can return values), and the other one contains a series of matlab statements that are to be executed exactly as if they were entered one by one in the command window.
Do the following:
– Make a Matlab function file sierpinski.m which includes only your function code:
function sierpinski(x,y,n)
hold on
if n == 0
fill(x,y,'r')
else
x2 = [(x(2)-x(1))/2, (x(2)-x(3))/2, x(3)+(x(2)-x(3))/2];
y2 = [y(1), y(3)/2, y(3)/2];
sierpinski(x2, y2, n-1)
end
Save the file to the current directory or a directory on the path.
– In the command window, enter the statement sierpinski([0,1,0.5],[0,0,1],2). The result is a figure window with a skewed red triangle. Not a Sierpinski triangle, but I guess the first step is done. ;-)
Instead of entering that statement in the command window, you can also make a Matlab script file. Edit a file with the name e.g. run_sierpinski.m, which contains the statement:
sierpinski([0,1,0.5],[0,0,1],2)
Again, save the file to the current directory or a directory on the path.
Now you can run the script, either by clicking the "Run" button in the GUI (green triangle or so), or by entering run_sierpinski in the command window. Either way, the result should be the same as entering the statement directly.

Matlab fopen fails to open file name passed as string variable but opens when passed as string

Hi all I'm trying to read in files using a variable and for some reason when I pass the same string as a variable it no longer opens. Below I try the same command simply swapping the variable for its contents and get different results? I also tried DEBLANK and STRTRIM from checking other questions.
f=fopen(fname,'r');
f
f =
-1
fname
fname =
/xchip/cga_home/amaro/Cranios/Segs/001-CN-001-CN-N.tsv
f=fopen('/xchip/cga_home/amaro/Cranios/Segs/001-CN-001-CN-N.tsv','r');
f
f=3
Hey sorry the example wasn't helpful. I solved this problem by reverting to an older version of matlab from 2013a to 2012b. Basically matlab 2013a was treating the file handle differently when passed as a variable.

Using matlabs save in functions

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