check dimensions for parameters

Hello, in a current setup I very often load datasets from an m file and write over current variables in the base workspace. My problem is that sometimes the dimensions differ and I then get problems with my simulations. Lets say I have a variable a = 1 in my workspace and then try to write a = [1 1; 1 1] from my m file. In that case I would like to cancel that and inform the user that there was a dimension mismatch for that variable. I tried some quite complicated way might work by using eval and evalin but there must be an easier way to fix that...can someone help me?

1 Comment

Stephen23
Stephen23 on 10 May 2016
Edited: Stephen23 on 10 May 2016
Avoid using eval, evalin, assignin, etc. They will only cause problems. Write functions, and pass the arguments properly. Checking input arguments is then trivial.
Guillaume's answer and explanation is spot on.

Sign in to comment.

Answers (1)

The answer is to change the way you work and rewrite your script as a function. Instead of dumping your dataset into the base workspace you would pass these inputs to your function, which in the first step would simply validate the inputs and issue an error message if they're the wrong shape.
For validating inputs, you can use validateattributes:
function [possibleoutput] = usedtobescript(a, b, c) %use better variable names
validateattributes(a, {'numeric'}, {'scalar'}); %issues an error if a is not numeric or scalar
%...
end

3 Comments

Hej thanks for the answer, I'm actuall wrote a script for that and used the code below...not exactly as you recommended...
dimensionError = {};
VariableInWorkspace = 1;
for i = 1:length(newData)
try
existingValue = evalin('base', newData{i});
existingValueSize = numel(existingValue);
catch
VariableInWorkspace = 0;
end
newValue = eval(newData{i});
newValueSize = numel(newValue);
if existingValueSize == newValueSize && VariableInWorkspace == 1
assignin('base', newData{i}, newValue);
elseif VariableInWorkspace == 0
assignin('base', newData{i}, newValue);
VariableInWorkspace = 1;
else
dimensionError{end+1,1} = newData{i};
end
end
but then I get problems using assignin
I also have to filter out all names as I have structures of different size as e.g. A.Ag.C.var1, A.Ag.D.var2, A.Cd.T.var3 and so on
As I said, use functions not scripts. And above all, do not use assignin, evalin, eval and co. They're not recommended for a reason. They cause more problem than they solve.
Use function inputs and outputs to pass variables between different pieces of code. It's much cleaner and it's easier to keep track of what is what. A function should not read or write variables in the base workspace.
As an aside, since scripts share the base workspace, there is no reason for a script to use evalin or assignin. The script can access the variable directly.

Sign in to comment.

Categories

Products

Asked:

on 10 May 2016

Edited:

on 10 May 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!