Accessing the Value of Variables in the Workspace

18 views (last 30 days)
Hello,
I currently have a list of arrays in the workspace that I loaded from a .dat file. It generates around 600 variables with large number of data in my workspace. My goal is to access the values of all those generated variables so that I can use it to generate some plots.
The problem is that I do not know how to access the value of the variables without knowing the names. I want to be able to access the data inside ´specific variables in the workspace without directly using the variable name.
The idea goes like this:
handles.workspace = evalin('base','who');
set(handles.unselecteddatalistbox,'String',handles.workspace); % adds the names of the variables in the workspace to a listbox in a GUI where I can select and move the name of the variables that i have selected inito another list.
% Once i have the names of the varariables that i want access in another variables call selectedDataStr, how can i access that informatoin?
for i = 1:length(selectedDataStr)
for j = 1:length(handles.workspace)
if strcmp(selectedDataStr(i),handles.workspace(j))
eval([selectedDataStr(i),',handles.workspace(j)']);
% this does not seem to work becuase even though i think matlab recognizes that selectedDataStr(i) is a string it does recognizes that i want the data from the name of the variables of that it is inside handles.workspace(j).
end
end
end
Please advice,
Alberto

Accepted Answer

Dennis
Dennis on 14 Mar 2019
You can load your data into a structure:
s=load('MyData.mat');
Now you can use fieldnames to obtain all variable names in your structure and acces them:
fnames=fieldnames(s); %fnames will be a cell
s.(fnames{1}) %how to obtain your data

More Answers (0)

Community Treasure Hunt

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

Start Hunting!