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
Related
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.
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.
load( lapFileSource, 'UntitledMeta_Data' );%My MetaData
universal={'TestType';'TestApparatus';'TestSystem Location';
'Configuration';'Wire condition';'Wire Type';'Circuit';};
u=11;
for o=drange(1:u)
if strcmp('',MetaData{o})
universal{o}='Null';
else
universal{o}=MetaData{o};
end
assignin('base','universal{o}',MetaData{o})
end
I am getting error to assignin the variable in workplace.
You need to read the documentation for the functions you use. From the documentation for assignin:
% The var input must be the array name only; it cannot contain array indices.
And, even more explicitly:
% assignin does not assign to specific elements of an array.
% The following statement generates an error:
X = 1:8;
assignin('base', 'X(3:5)', -1);
You can either shift your assignin call outside of the for loop and pass your full array to the base workspace, or you can follow the advice of the documentation and utilize evalin.
For example:
function trialcode
n = 5;
myarray = cell(1, 5);
for ii = 1:n;
if mod(ii,2)
myarray{ii} = 'odd';
else
myarray{ii} = 'even';
end
mycmd = sprintf('evalinarray{%u} = ''%s'';', ii, myarray{ii});
evalin('base', mycmd);
end
assignin('base','assigninarray', myarray)
end
The resulting arrays, evalinarray and assigninarray, are equivalent. Of the two, I would recommend using assignin as it is a lot more robust and explicit than the evalin approach.
I am working on a simple System Object in Matlab/Simulink.
It looks like this :
classdef realtime_header_detectorSO < matlab.System & matlab.system.mixin.Propagates
% correlateHeader
%
% This template includes the minimum set of functions required
% to define a System object with discrete state.
properties
Header
%nrOfBitsInPreviousStep=0;
s=100;
d=zeros(1,s);
end
properties (DiscreteState)
end
properties (Access = private)
max_nr_of_packets=20;
max_packet_length_in_bytes=300;
current_packet=1;
% Pre-computed constants.
% h = commsrc.pn('GenPoly', [9 5 0],'NumBitsOut', 8,'InitialStates',ones(1,9));
% data=logical(zeros(max_nr_of_packets,max_packet_length_in_bytes*8));
end
methods (Access = protected)
function setupImpl(obj,u)
% Implement tasks that need to be performed only once,
% such as pre-computed constants.
end
function [maxCorr]= stepImpl(obj,u)
eml.extrinsic('num2str');
coder.extrinsic('sprintf');
% Implement algorithm. Calculate y as a function of
% input u and discrete states.
%y = size(u,1);
symbols=sign(u);
c=abs(conv(flipud(obj.Header),[symbols; symbols]));
maxCorr=max(c);
% maxCorr
if(maxCorr==36)
idx36=find(c(1:size(symbols,1))==36);
disp('header(s) detected at the following location(s) in bytes:');
disp(sprintf('%15.4f \n',idx36/8));
nrOfSymbols=size(symbols,1);
disp(['out of nr. of total symbols: ' num2str(nrOfSymbols)]);
disp('------');
% maxCorr
end
% y=obj.pBufferIdx;
end
function resetImpl(obj)
% Initialize discrete-state properties.
end
function varargout = isOutputFixedSizeImpl(~)
varargout = {true};
end
function varargout = getOutputSizeImpl(obj)
varargout = {[1 1]};
end
end
end
However when I compile/run it I get the following error:
The System object name 'realtime_header_detectorSO' specified in MATLAB System block 'freqScanningRT/Sync and
Find Header/detect header' is invalid.
Caused by:
Undefined function or variable 's'.
However (!) the following code compiles and runs just fine :
classdef realtime_header_detectorSO < matlab.System & matlab.system.mixin.Propagates
% correlateHeader
%
% This template includes the minimum set of functions required
% to define a System object with discrete state.
properties
Header
%nrOfBitsInPreviousStep=0;
s=100;
% d=zeros(1,s);
end
properties (DiscreteState)
end
properties (Access = private)
max_nr_of_packets=20;
max_packet_length_in_bytes=300;
current_packet=1;
% Pre-computed constants.
% h = commsrc.pn('GenPoly', [9 5 0],'NumBitsOut', 8,'InitialStates',ones(1,9));
% data=logical(zeros(max_nr_of_packets,max_packet_length_in_bytes*8));
end
methods (Access = protected)
function setupImpl(obj,u)
% Implement tasks that need to be performed only once,
% such as pre-computed constants.
end
function [maxCorr]= stepImpl(obj,u)
eml.extrinsic('num2str');
coder.extrinsic('sprintf');
% Implement algorithm. Calculate y as a function of
% input u and discrete states.
%y = size(u,1);
disp(obj.s);
symbols=sign(u);
c=abs(conv(flipud(obj.Header),[symbols; symbols]));
maxCorr=max(c);
% maxCorr
if(maxCorr==36)
idx36=find(c(1:size(symbols,1))==36);
disp('header(s) detected at the following location(s) in bytes:');
disp(sprintf('%15.4f \n',idx36/8));
nrOfSymbols=size(symbols,1);
disp(['out of nr. of total symbols: ' num2str(nrOfSymbols)]);
disp('------');
% maxCorr
end
% y=obj.pBufferIdx;
end
function resetImpl(obj)
% Initialize discrete-state properties.
end
function varargout = isOutputFixedSizeImpl(~)
varargout = {true};
end
function varargout = getOutputSizeImpl(obj)
varargout = {[1 1]};
end
end
end
So I can access s in the stepImpl(obj,u) as obj.s but I cannot access s inside the properties block, where it is defined !
Now this is confusing.
Is there way to access s inside the properties block ?
The problem is that I have to use the properties block because if I try this :
function setupImpl(obj,u)
% Implement tasks that need to be performed only once,
% such as pre-computed constants.
d=zeros(1,obj.s);
end
then I get :
Error due to multiple causes.
Caused by:
Problem creating simulation target MEX-file for model 'freqScanningRT'.
Simulink detected an error
'Computed maximum size is not bounded.
Static memory allocation requires all sizes to be bounded.
The computed size is [1 x :?].'.
The error occurred for MATLAB System block 'freqScanningRT/Sync and Find Header/detect header'. See line
34, column 15 in file
'path/realtime_header_detectorSO.m'.
The error was detected during code generation phase.
Start code generation report.
To prevent this error, use one of the following:
* Modify the System object to avoid code that does not support code generation.
* Change 'Simulate using' parameter to 'Interpreted Execution'.
Any idea how to refer to variables in the properties blocks ?
There must be a way to do this.
You should be able to use
properties
s = 100;
d = zeros(1,100);
end
right? If you already have the 100 as a default for s, you should also be able to provide this as part of the default for d.
I'm guessing that you're trying to avoid doing that because you feel uncomfortable repeating the "100". But I'd also guess that really, "100" is some sort of magic number that your system depends on; so really, you should try to pull it out as a constant in any case, whether it's repeated or not.
So in that case, you might improve things with
properties (Constant)
MYCONSTANT = 100;
end
properties
% Reference Constant class properties with the class name
s = realtime_header_detectorSO.MYCONSTANT;
d = zeros(1, realtime_header_detectorSO.MYCONSTANT);
end
You're not going to be able to do what you're originally trying to do - it's not possible to reference the name of one property within the property definition block when defining another property (even though you can perfectly well reference it within a method). I guess I understand why you find that confusing - but to clear up your confusion, note that you have no guarantee over the order in which MATLAB instantiates default values for properties. If it tried to create d before it had created s, it would obviously fail.
You can avoid this problem all together by initializing the properties in the constructor.
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