-1

I have a simple but interesting question. i tired hard to google it but my google got upset and giving me the same results... i wanted to know is it possible to Update a constant variable form workspace command..

A Simple Example:

function y =StupidQuestion

a = 10; % some value
b =[5,6,7;1,2,8];  % some value
y = b*a  % some operation

I forget to tell you that we can do it with simulink block by using below command

set_param('obj', 'parameter1', value1, 'parameter2', value2, ...)

i Want to use the assigned value for 3 weeks and without any reason i wants to change my values [a,b] to other but through command windows. any Idea. Waiting for your interesting Reply...................

aurelius
  • 3,946
  • 7
  • 40
  • 73
user2851655
  • 133
  • 2
  • 9
  • 2
    Do you know how to pass variables into a function? (e.g. `function y = myfunc(a,b)`?). Because if that's not what you're looking for I have no idea what you're talking about. – nkjt Oct 01 '14 at 10:40
  • I know i can pass a variable (a,b). but here every time i need to give a and b Values whenever i runs a function. – user2851655 Oct 01 '14 at 10:45
  • I doubt that this can be done. I actually thinks that it is a good thing that you cannot edit in the source code unless you are inside the source code itself. However, if you truly wanted to do this, I think that you could write a function that can parse the source code and make changes where necessary. This is in my opinion a tedious operation and if possible you should always do the change in the source code itself. Matlab have probably implmented this in some smart way includig reading default parameters at start and storing them for every session and/or adding extra startup commands or so. – patrik Dec 22 '14 at 14:13

2 Answers2

2

You can set defaults for the inputs:

function y = foo(a,b)
if nargin < 1 || isempty(a), a = 10;            end
if nargin < 2 || isempty(b), b = [5,6,7;1,2,8]; end
y = b*a
end

You can call foo() without inputs (and it will use the defaults for a and b) or supply your own values as: foo(12), foo(12,[10,20]), foo([],[23,23]), etc...

Oleg
  • 10,406
  • 3
  • 29
  • 57
  • Dear Oleg. Thanks for your Effort. With your approch i will not able to update my variables permenantly. like once i run with input Argument **foo(15)** than i can only pass a Varable but will not be able to update it . as in case of Simulink Block. if i want to update a gain block permenantly(change a varible). i can just simply do it by 'set_param('fileName/blockName', 'ParameterName', '500')' . then it will update my block from previous value to updated value..... By the I am Sorry for uncleard explanation in my question.. – user2851655 Oct 01 '14 at 11:57
  • @user2851655 Just edit the .m file (or store the variables in a block in Simulink and change from there, but I never used Simulink so don't take me literally here) – Oleg Oct 01 '14 at 12:01
  • i thought we can do anything in matlab. but You are right..... by the i got a new idea i will add some MAT file and update it. thanks but still do u have any other idea... and Thanks alot dear........ – user2851655 Oct 01 '14 at 12:10
0

A possible way is to save some variables in an external file. Note that in this case a and b are only in the function workspace (you won't see their values unless you load the contents of test.mat separately). I'm passing the filename in rather than hard-coding it in case you need to switch between multiple settings.

Personally I would prefer to have a human-readable data file, but the concept remains the same (you'd just need some parser function which returned values for a and b given a file).

a = 10; % some value
b =[5,6,7;1,2,8];  % some value
save('test.mat','a','b');
clear a b;

function y = savedvariables(filename)
    load(filename);
    y = b*a;  % some operation
end

y = savedvariables('test.mat');
nkjt
  • 7,825
  • 9
  • 22
  • 28