Manipulate unknown variables - avoid using eval()

1 view (last 30 days)
Jon
Jon on 30 Apr 2015
Commented: Stephen23 on 1 May 2015
How can I avoid using eval in this situation:
  • I have data imported and formatted as : X 2 matrices
  • The names of the matrices may change in each import
  • I need to manipulate one of the columns for each matrix on each run
ex: run 1
>> who
Your variables are:
AcceleratorPosition Battery1Voltage
AmbientTemperature BatterySide0x28A0x29
>>
run 2
>> who
Your variables are:
DCDCLVCurrent EngineActualTorque
DCDCLVVoltage EngineFuelRate
>>
without using eval(), and without having the vector names in my script, how would I accomplish this:
>> DCDCLVCurrent(:,1) = DCDCLVCurrent(:,1) * 0.5;
>> DCDCLVVoltage(:,1) = DCDCLVVoltage(:,1) * 0.5;
>> EngineActualTorque(:,1) = EngineActualTorque(:,1) * 0.5;
>> EngineFuelRate(:,1) = EngineFuelRate(:,1) * 0.5;

Answers (1)

Image Analyst
Image Analyst on 1 May 2015
Do you not have the run1.m and run2.m scripts? I mean I can't just take two arbitrary scripts, and run them and be able to expect to do the same things with them. If I gave you a script, it might produce something totally different, then there's no way you'd be able to add 0.5 to 4 output variables arrays. What if I didn't even give you 4 arrays at the output? So you must have control over them (have source code), and if you have that then you can accept the results into 4 output arrays that you give names to, like
[array1, array2, array3, array4] = run1();
array1 = array1 + 0.5;
array2 = array2 + 0.5;
array3 = array3 + 0.5;
array4 = array4 + 0.5;
Just modify your scripts or functions to have them return 4 arrays as output arguments. Then we really don't care what they are called internal to that function.
  4 Comments
Jon
Jon on 1 May 2015
Stephen, I think you're probably correct and this should be in a function. Still interested to know if someone has a script style suggestion though.
Stephen23
Stephen23 on 1 May 2015
How do you generate the variable names run1 and run2 without using eval ?

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!