1

I need a function that selects specific variables in my workspace (using wildcards), assigns their values (=value of these variables) to a new variable, (AND THIS IS THE PART WHERE I GET STUCK) manipulates them, and then saves this new variables under the old variable name.

varnames= who('*_small_*');

for n=length(varnames)
    new = str2cell(varnames(n);  %THIS STEP DOES NOT WORK.
    %manipulation of those values;
    filename=varnames(n);
    save(filename,new);
end;

Anybody idea how to call the variables that I need and extract their values? Probably my solution is not the best, so feel free to suggest an alternative to who.

thanks a lot!

katrin01
  • 11
  • 2
  • 1
    Please post code without syntax errors. Also, the question and the code do not match in meaning, so please fix that too. – Mad Physicist Aug 16 '16 at 15:40
  • 3
    The best alternative would be to structure your code so that you know exactly what variables you need to manipulate. Where are these variables generated that you don't know what they are? – beaker Aug 16 '16 at 16:03
  • Sorry but I cannot help commenting. Your approach will make you lose all the control of your code. You do not know when variables are assigned, when they are overwritten or where they are used. Please think over your design once more and take cells into account as well. Apart from this. There are numerous syntax errors in your code. Please fix these. I also have troubles helping since the steps you have problems with are not even needed. Please update this part as well to improve understanding. – patrik Aug 17 '16 at 05:53
  • Sorry, I am new to this forum. Thanks for your feedback and helpfull comments, my next post will be more clear! – katrin01 Aug 18 '16 at 10:47

1 Answers1

0

1.Don't use str2cell. instead use curly brackets.

varname = varnames{n};

2.The eval function is not best practice, but it may be the simplest way to solve this.

eval(sprintf('new = %s;',varname)); %assign variable with varname to new

3.If you are just overwriting the variable in the workspace, don't use save. Just use eval again.

eval(sprintf('%s = new;',varname)); %assign 'new' value to varname
Trogdor
  • 1,346
  • 9
  • 16
  • 1
    You should try to [evoid `eval` whenever possible](http://stackoverflow.com/questions/32467029/how-to-put-these-images-together/32467170#32467170). Here as well. If it's not "best practise", don't teach it to others I'd say. Teach them the best practise instead. – Adriaan Aug 16 '16 at 17:26
  • 2
    If you have an alternative that avoids the use of `eval` I'm interested to see it. In the scope of this question, I'm not aware of a way around it without increasing the complexity signifcantly – Trogdor Aug 16 '16 at 18:41
  • 1
    That's because the desired behavior is very poor, requiring a poor solution. – sco1 Aug 17 '16 at 12:19
  • Thank you Trogdor for the help, quick and dirty worked! :) excaza your feedback was less constructive. "desired behavior is very poor". That is a bit offensive, I guess. The person to judge should know context and purpose of the code. ...so far so good, next time I use this forum, I'll try a neutral user name :D – katrin01 Aug 18 '16 at 15:00