Referencing variables in the workspace

9 views (last 30 days)
Hello all,
I am using a script which accesses an mdf file and then stores the channels chosen into arrays/mat in matlab. While it does this, it adds extensions to the end of the channel name ex: Engine_speed_24. I can see this variable in the workspace. I want to know is there a way of accessing or referencing these variables which are generated by an external script/function in my main code? I need to plot these variables but I don't want to type in the name of the variable each time, as the extension might change if the channel changes.
Ex - Channel names - Engine_speed, Engine_torque. Run func -> extracts these channels from mdf file -> stores variables as Engine_speed_XX and Engine_torque_YY, time_XX and time_YY
I need a way to plot them against time as I don't know what the extensions might be.
Thanks
  2 Comments
Image Analyst
Image Analyst on 6 Nov 2014
What is the extension? If it's .mdf, that's not going to change depending on what's inside, since you can't know what's inside until you actually open the .mdf file. So is the extension the "_24"? Or something else? Since you have access to the m file, it's best if you have it return a variable with the same name each time rather than do whatever it does to make variable names that are dependent on something in the file.
Aditya
Aditya on 7 Nov 2014
I am using a code/script I got from the mathworks site : http://www.mathworks.com/matlabcentral/fileexchange/9622-mdf-import-tool-and-function
The code automatically adds an extension. Ideally I would want it to keep the same channel name as the .mdf file but it adds an extension. I shall have to tweak this code for that.

Sign in to comment.

Accepted Answer

Chad Greene
Chad Greene on 6 Nov 2014
Does this help?
x = 1;
y = 2;
s = who;
s(1)
  5 Comments
Aditya
Aditya on 7 Nov 2014
Thank you for your help Chad!
I had another question - How do I read or extract numbers from a string, for example if the variable name is Engine_Speed_24, I want to store the number at the end of the '_' separately (i.e. 24). Basically to save the extension number so I can use it later. Also the string length will vary each time but will always end with a number.
Chad Greene
Chad Greene on 7 Nov 2014
clear
Engine_Speed_24 = 5;
w = who;
w{1}(end-1:end)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!