Modify Simulink parameters from matlab workspace - matlab

I have a program that allows me to transfer all the block parameters from my simulink file to the matlab workspace which is this part :
function TransVar(SimulinkName) %program takes the simulink file name as argument
AE=find_system(SimulinkName); % AE is an array containing in the first cell the name of the
simulink file then the name
% of the blocks in this format : 'SimulinkName/blockName'
for i = 2:length(AE); % i start at 2 because first element of AE is the name of the file
A = strfind(AE{i,1},'/')%find the rank of the / in the string
newName = AE{i,1}(A+1:end);% Gives newName the name of the block
ParList(i-1,1) = {newName}; % Assign the block name to a cell array
assignin('base','ParList',ParList) % Export it to the matlab workspace
newName = regexprep(newName,'\W','');
newName = regexprep(newName,'[éè]','e');
newName = newName(isstrprop(newName,'alphanum'));
ParName(i-1,1) = {newName}; %change the name of the block so that it can be used as a field
assignin('base','ParName',ParName) % export it to the workspace
p = Simulink.Mask.get(AE{i,1});
if isempty(p)
Parametre.(newName) = [];
else
Parametre.(newName)=p.getWorkspaceVariables; %get the variables from the simulink block and
assign it ot the field of it's
%name in the structure parameter
assignin('base','Parametre',Parametre) % Export it to matlab workspace
end
end
Now all the variables have been exported to matlab workspace and can be modified by the user. what i want to do next is once these variables have been changed, I want to be able to change them in the simulink file for that i use this part :
Parametre = evalin('base','Parametre'); %Assign to parameter the struct parameter from the
matlab workspace
ParName = evalin('base','ParName');
ParList = evalin('base','ParList');
%Update the variables in the simulink file
for i = 1:length(fieldnames(Parametre))
if isempty(Parametre.(ParName{i,1})) %if the field has no variables then it will do nothing
and go to the next field
else
for b = 1:length(Parametre.(ParName{i,1}))
if length(Parametre.(ParName{i,1})(b).Value) >= 100 %if one of the variable in the fied has
more than 100 values it won't
% be updated as it is not a usefull one.
else
if isnumeric(get_param([SimulinkName '/' ParList{i,1}],Parametre.(ParName{i,1})(b).Name)) == 0
%if a variables isn't a number
%but a string when imported from simulink then it won't be updated
elseif isempty(Parametre.(ParName{i,1})(b).Value)
set_param([SimulinkName '/' ParList{i,1}],Parametre.(ParName{i,1})(b).Name,'[]')% if variable
is empty it gives the value
% []
save_system([SimulinkName])
else % if the variable in the field has a proper value it will update the one in the simulink
file then save the simulink file
set_param([SimulinkName '/' ParList{i,1}],Parametre.(ParName{i,1})(b).Name,(['['
num2str(Parametre.(ParName{i,1})(b).Value) ']' ]))
save_system([SimulinkName])
end
end
end
end
end
The problem that is faced is that there is no time for the user to actually change the variables in the workspace. And if i try to pause the function with an uiwait the variables cannot be modified in the workspace, you can see them but not acces them.

Related

Save the data in a form of three columns in text files

This function reads the data from multiple mat files and save them in multiple txt files. But the data (each value) are saved one value in one column and so on. I want to save the data in a form of three columns (coordinates) in the text files, so each row has three values separated by space. Reshape the data before i save them in a text file doesn't work. I know that dlmwrite should be modified in away to make newline after three values but how?
mat = dir('*.mat');
for q = 1:length(mat)
load(mat(q).name);
[~, testName, ~] = fileparts(mat(q).name);
testVar = eval(testName);
pos(q,:,:) = testVar.Bodies.Positions(1,:,:);
%pos=reshape(pos,2,3,2000);
filename = sprintf('data%d.txt', q);
dlmwrite(filename , pos(q,:,:), 'delimiter','\t','newline','pc')
end
My data structure:
These data should be extracted from each mat file and stored in the corresponding text files like this:
332.68 42.76 42.663 3.0737
332.69 42.746 42.655 3.0739
332.69 42.75 42.665 3.074
A TheMathWorks-trainer once told me that there is almost never a good reason nor a need to use eval. Here's a snippet of code that should solve your writing problem using writematrix since dlmwrite is considered to be deprecated.
It further puts the file-handling/loading on a more resilient base. One can access structs dynamically with the .(FILENAME) notation. This is quite convenient if you know your fields. With who one can list variables in the workspace but also in .mat-files!
Have a look:
% path to folder
pFldr = pwd;
% get a list of all mat-files (returns an array of structs)
Lst = dir( fullfile(pFldr,'*.mat') );
% loop over files
for Fl = Lst.'
% create path to file
pFl = fullfile( Fl.folder, Fl.name );
% variable to load
[~, var2load, ~] = fileparts(Fl.name);
% get names of variables inside the file
varInfo = who('-file',pFl);
% check if it contains the desired variables
if ~all( ismember(var2load,varInfo) )
% display some kind of warning/info
disp(strcat("the file ",Fl.name," does not contain all required varibales and is therefore skipped."))
% skip / continue with loop
continue
end
% load | NO NEED TO USE eval()
Dat = load(pFl, var2load);
% DO WHATEVER YOU WANT TO DO
pos = squeeze( Dat.(var2load)(1,:,1:2000) );
% create file name for text file
pFl2save = fullfile( Fl.folder, strrep(Fl.name,'.mat','.txt') );
writematrix(pos,pFl2save,'Delimiter','\t')
end
To get your 3D-matrix data into a 2D matrix that you can write nicely to a file, use the function squeeze. It gets rid of empty dimensions (in your case, the first dimension) and squeezes the data into a lower-dimensional matrix
Why don't you use writematrix() function?
mat = dir('*.mat');
for q = 1:length(mat)
load(mat(q).name);
[~, testName, ~] = fileparts(mat(q).name);
testVar = eval(testName);
pos(q,:,:) = testVar(1,:,1:2000);
filename = sprintf('data%d.txt', q);
writematrix(pos(q,:,:),filename,'Delimiter','space');
end
More insight you can find here:
https://www.mathworks.com/help/matlab/ref/writematrix.html

write wrapper for matlabs save function

I would to write a wrapper for the save function of matlab with predefined options (predefined version in my case to allow saving large files), i.e. something like this
save('parameters.mat', 'some', 'parameters', 'here', '-v.3');
should turn into this
save_large('parameters.mat', 'some', 'parameters', 'here');
where save_large is a wrapper for save with version set to '-v7.3':
function [ ] = save_large( filename, varargin )
%varargin to allow for multiple variable storing?
%what to write here to save all variables (declared as chars) stored in
%the workspace where 'save_large' was called with version set to '-v7.3'?
end
Because the variables won't exist in the scope of the function save_large, you will have to use evalin to get the variable from the "caller" workspace.
Using try we can also ensure that the variable exists in the caller workspace.
To get the correct variable names within your .mat file, we could either use the (discouraged) eval function, or the below method which assigns all of the variables to a struct, and then use the -struct flag in save.
function save_large(filename, varargin)
% Set up struct for saving
savestruct = struct();
for n = 1:numel(varargin)
% Test if variable exists in caller workspace
% Do this by trying to assign to struct
% Use parentheses for creating field equal to string from varargin
try savestruct.(varargin{n}) = evalin('caller', varargin{n});
% Successful assignment to struct, no action needed
catch
warning(['Could not find variable: ', varargin{n}]);
end
end
save(filename, '-struct', 'savestruct', '-v7.3');
end
Example
% Create dummy variables and save them
a = magic(3);
b = 'abc';
save_large test.mat a b;
% Clear workspace to get rid of a and b
clear a b
exist a var % false
exist b var % false
% Load from file
load test.mat
% a and b in workspace
exist a var % true
exist b var % true

Is it possible to change an anonymous function keeping the workspace?

What I would like to be able to do is programmatically change an anonymous function for example by changing all the plus signs to multiplication signs in the function. This example can in many cases this can be done as follows:
function f2 = changefunction(f1)
fs = func2str(f1);
fs(fs=='+') = '*';
f2 = str2func(fs);
end
But consider the example
f = #(x) x+5;
a = 5;
g = #(x) x+a;
Both f and g will be anonymous functions that adds 5 to whatever you plug into it; however only f will be changed correctly by the changefunction function, whereas g will be changed into a function that will err on any input.
So my question is is it possible to extract the workspace from the function handle and retain it in the new function handle created? I need to do it programmatically and preferably without using the built-in function functions!
One naive implementation is to replace str2func with eval so you are not running into str2func's roadblock of not allowing access to local variables. We can use functions to obtain the workspace information for the input function handle.
For example:
a = 5;
f = #(x) x+a;
finfo = functions(f)
Yields:
finfo =
struct with fields:
function: '#(x)x+a'
type: 'anonymous'
file: 'X:\testcode-matlab\testcode.m'
workspace: {[1×1 struct]}
within_file_path: 'testcode'
Where workspace is a cell array containing a structure (come on MathWorks...) containing all of the variables in your function handle's namespace:
>> wspace = finfo.workspace{1}
wspace =
struct with fields:
a: 5
Using this functionality, the naive solution is to loop through the variables in this workspace, assign them in the namespace of changefunction, then use eval to generate the new function handle.
For example:
function f2 = changefunction_new(f1)
tmp = functions(f1);
workspacevars = tmp.workspace{1};
varnames = fieldnames(workspacevars);
for ii = 1:length(varnames)
evalstr = sprintf('%s = %d;', varnames{ii}, workspacevars.(varnames{ii}));
eval(evalstr);
end
fs = func2str(f1);
fs(fs=='+') = '*';
f2 = eval(fs);
end
Here I'm assuming that the variables are going to be strictly numeric. You can add logic to check the class of the data to be generated if this is not always the case.
With this we have:
a = 5;
g = #(x) x+a;
test1 = changefunction(g);
test2 = changefunction_new(g);
>> g(1)
ans =
6
>> test1(1)
Undefined function or variable 'a'.
Error in testcode>#(x)x*a
>> test2(1)
ans =
5
All that being said, the best solution really is to just explicitly define your function handles. It may be a pain but it's much easier to understand and debug.
A few caveats:
Because eval arbitrarily executes all code passed to it, it can be a very dangerous function that must be used with care.
The documentation for functions warns against using it programmatically, so take care to check behavior as MATLAB versions change:
Use the functions function for querying and debugging purposes only.
Note: Do not use functions programmatically because its behavior could change in subsequent MATLAB® releases.
One possible way to do this is to save the function handle to a .mat file (using the -v7.3 flag so that it creates an easily-modifiable HDF5 file), modify the struct within the file that contains the workspace data for the anonymous function (using the HDF5 tools built into MATLAB), and then load the anonymous function again from the file.
Here is a little function which does exactly that (and it works for relatively simple variable types)
function result = modifyfunc(f, varname, value)
% modifyfunc - Modify the workspace of an anonymous function
%
% INPUTS:
% f: Function Handle, Anonymous function to modify
% varname: String, Name of the variable to modify
% value: Data to replace the specified variable
% If the value is a struct, recursively modify the function handle
if isstruct(value)
fields = fieldnames(value);
result = f;
% Modify each field separately
for k = 1:numel(fields)
% Append the fieldname to the variable name and modify
name = [varname, '.', fields{k}];
result = modifyfunc(result, name, value.(fields{k}));
end
return;
end
% Write the anonymous function to an HDF5 file
fname = tempname;
save(fname, 'f', '-mat', '-v7.3');
% Replace any "." in the variable name with "/" to construct the HDF5 path
varname = strrep(varname, '.' , '/');
% Now modify the data in the file
h5write(fname, ['/#refs#/e/' varname], value);
% Load the modified function handle from the file
result = load(fname, '-mat');
result = result.f;
% Remove the temporary file
delete(fname);
end
And you can use it like:
a = 1;
b = struct('field', 2);
f = #(x)disp(a + b.field + x);
f(10)
% 13
f2 = modifyfunc(f, 'a', 2);
f2(10)
% 14
f3 = modifyfunc(f2, 'b.field', 3);
f3(10)
% 15
b.field = 4;
f4 = modifyfunc(f3, 'b', b);
f4(10)
% 16
Some caveats include:
The replacement data must be the same size as the original data
This relies upon the format of the .mat file which for anonymous functions is completely undocumented so it could fail in future releases.
This currently doesn't work for variables in the function workspace that are cell arrays.

Matlab: renaming workspace elements from command window?

The GUI of Matlab allows me to rename any element in the workspace by right-clicking on the element and selecting the 'rename' option. Is it possible to do this from the command window as well?
These are things you can easily test for yourself, and you should do so. That is the best way to learn, to discover.
Regardless, the answer is no, you cannot change a variable name in that way from the command window. The command window is mainly for keyboard input only.
Edit: The question was apparently about doing that change by a command in the command window, not to be done via a mouse. (Why not tell us that up front?)
There is no explicit command that does such a rename. However, nothing stops you from writing it yourself. For example...
function renamevar(oldname,newname)
% renames a variable in the base workspace
% usage: renamevar oldname newname
% usage: renamevar('oldname','newname')
%
% renamevar is written to be used as a command, renaming a single
% variable to have a designated new name
%
% arguments: (input)
% oldname - character string - must be the name of an existing
% variable in the base matlab workspace.
%
% newname - character string - the new name of that variable
%
% Example:
% % change the name of a variable named "foo", into a new variable
% % with name "bahr". The original variable named "foo" will no
% % longer be in the matlab workspace.
%
% foo = 1:5;
% renamevar foo bahr
% test for errors
if nargin ~= 2
error('RENAMEVAR:nargin','Exactly two arguments are required')
elseif ~ischar(oldname) || ~ischar(newname)
error('RENAMEVAR:characterinput','Character input required - renamevar is a command')
end
teststr = ['exist(''',oldname,''',''var'')'];
result = evalin('base',teststr);
if result ~= 1
error('RENAMEVAR:doesnotexist', ...
['A variable named ''',oldname,''' does not exist in the base workspace'])
end
% create the new variable
str = [newname,' = ',oldname,';'];
try
evalin('base',str)
catch
error('RENAMEVAR:renamefailed','The rename failed')
end
% clear the original variable
str = ['clear ',oldname];
evalin('base',str)
You can rename variables in the command window as follows:
%# create a variable
a = 3;
%# rename a to b
b = a;clear('a');
EDIT
If you want to rename your variable to another variable stored in a string, you can use ASSIGNIN
a = 3;
newVarName = 'b';
assignin('base',newVarName,a);
clear('a') %# in case you want to get rid of the variable a

deleting variables from a .mat file

Does anyone here know how to delete a variable from a matlab file? I know that you can add variables to an existing matlab file using the save -append method, but there's no documentation on how to delete variables from the file.
Before someone says, "just save it", its because I'm saving intermediate processing steps to disk to alleviate memory problems, and in the end there will be almost 10 GB of intermediate data per analysis routine. Thanks!
Interestingly enough, you can use the -append option with SAVE to effectively erase data from a .mat file. Note this excerpt from the documentation (bold added by me):
For MAT-files, -append adds new variables to the file or replaces the saved values of existing variables with values in the workspace.
In other words, if a variable in your .mat file is called A, you can save over that variable with a new copy of A (that you've set to []) using the -append option. There will still be a variable called A in the .mat file, but it will be empty and thus reduce the total file size.
Here's an example:
>> A = rand(1000); %# Create a 1000-by-1000 matrix of random values
>> save('savetest.mat','A'); %# Save A to a file
>> whos -file savetest.mat %# Look at the .mat file contents
Name Size Bytes Class Attributes
A 1000x1000 8000000 double
The file size will be about 7.21 MB. Now do this:
>> A = []; %# Set the variable A to empty
>> save('savetest.mat','A','-append'); %# Overwrite A in the file
>> whos -file savetest.mat %# Look at the .mat file contents
Name Size Bytes Class Attributes
A 0x0 0 double
And now the file size will be around 169 bytes. The variable is still in there, but it is empty.
10 GB of data? Updating multi-variable MAT files could get expensive due to MAT format overhead. Consider splitting the data up and saving each variable to a different MAT file, using directories for organization if necessary. Even if you had a convenient function to delete variables from a MAT file, it would be inefficient. The variables in a MAT file are layed out contiguously, so replacing one variable can require reading and writing much of the rest. If they're in separate files, you can just delete the whole file, which is fast.
To see this in action, try this code, stepping through it in the debugger while using something like Process Explorer (on Windows) to monitor its I/O activity.
function replace_vars_in_matfile
x = 1;
% Random dummy data; zeros would compress really well and throw off results
y = randi(intmax('uint8')-1, 100*(2^20), 1, 'uint8');
tic; save test.mat x y; toc;
x = 2;
tic; save -append test.mat x; toc;
y = y + 1;
tic; save -append test.mat y; toc;
On my machine, the results look like this. (Read and Write are cumulative, Time is per operation.)
Read (MB) Write (MB) Time (sec)
before any write: 25 0
first write: 25 105 3.7
append x: 235 315 3.6
append y: 235 420 3.8
Notice that updating the small x variable is more expensive than updating the large y. Much of this I/O activity is "redundant" housekeeping work to keep the MAT file format organized, and will go away if each variable is in its own file.
Also, try to keep these files on the local filesystem; it'll be a lot faster than network drives. If they need to go on a network drive, consider doing the save() and load() on local temp files (maybe chosen with tempname()) and then copying them to/from the network drive. Matlab's save and load tend to be much faster with local filesystems, enough so that local save/load plus a copy can be a substantial net win.
Here's a basic implementation that will let you save variables to separate files using the familiar save() and load() signatures. They're prefixed with "d" to indicate they're the directory-based versions. They use some tricks with evalin() and assignin(), so I thought it would be worth posting the full code.
function dsave(file, varargin)
%DSAVE Like save, but each var in its own file
%
% dsave filename var1 var2 var3...
if nargin < 1 || isempty(file); file = 'matlab'; end
[tfStruct,loc] = ismember({'-struct'}, varargin);
args = varargin;
args(loc(tfStruct)) = [];
if ~all(cellfun(#isvarname, args))
error('Invalid arguments. Usage: dsave filename <-struct> var1 var2 var3 ...');
end
if tfStruct
structVarName = args{1};
s = evalin('caller', structVarName);
else
varNames = args;
if isempty(args)
w = evalin('caller','whos');
varNames = { w.name };
end
captureExpr = ['struct(' ...
join(',', cellfun(#(x){sprintf('''%s'',{%s}',x,x)}, varNames)) ')'];
s = evalin('caller', captureExpr);
end
% Use Java checks to avoid partial path ambiguity
jFile = java.io.File(file);
if ~jFile.exists()
ok = mkdir(file);
if ~ok;
error('failed creating dsave dir %s', file);
end
elseif ~jFile.isDirectory()
error('Cannot save: destination exists but is not a dir: %s', file);
end
names = fieldnames(s);
for i = 1:numel(names)
varFile = fullfile(file, [names{i} '.mat']);
varStruct = struct(names{i}, {s.(names{i})});
save(varFile, '-struct', 'varStruct');
end
function out = join(Glue, Strings)
Strings = cellstr(Strings);
if length( Strings ) == 0
out = '';
elseif length( Strings ) == 1
out = Strings{1};
else
Glue = sprintf( Glue ); % Support escape sequences
out = strcat( Strings(1:end-1), { Glue } );
out = [ out{:} Strings{end} ];
end
Here's the load() equivalent.
function out = dload(file,varargin)
%DLOAD Like load, but each var in its own file
if nargin < 1 || isempty(file); file = 'matlab'; end
varNames = varargin;
if ~exist(file, 'dir')
error('Not a dsave dir: %s', file);
end
if isempty(varNames)
d = dir(file);
varNames = regexprep(setdiff(ls(file), {'.','..'}), '\.mat$', '');
end
out = struct;
for i = 1:numel(varNames)
name = varNames{i};
tmp = load(fullfile(file, [name '.mat']));
out.(name) = tmp.(name);
end
if nargout == 0
for i = 1:numel(varNames)
assignin('caller', varNames{i}, out.(varNames{i}));
end
clear out
end
Dwhos() is the equivalent of whos('-file').
function out = dwhos(file)
%DWHOS List variable names in a dsave dir
if nargin < 1 || isempty(file); file = 'matlab'; end
out = regexprep(setdiff(ls(file), {'.','..'}), '\.mat$', '');
And ddelete() to delete the individual variables like you asked.
function ddelete(file,varargin)
%DDELETE Delete variables from a dsave dir
if nargin < 1 || isempty(file); file = 'matlab'; end
varNames = varargin;
for i = 1:numel(varNames)
delete(fullfile(file, [varNames{i} '.mat']));
end
The only way of doing this that I know is to use the MAT-file API function matDeleteVariable. It would, I guess, be quite easy to write a Fortran or C routine to do this, but it does seem like a lot of effort for something that ought to be much easier.
I suggest you load the variables from the .mat file you want to keep, and save them to a new .mat file. If necessary, you can load and save (using '-append') in a loop.
S = load(filename, '-mat', variablesYouWantToKeep);
save(newFilename,'-struct',S,variablesYouWantToKeep);
%# then you can delete the old file
delete(filename)