Accessing the Value of Variables in the Workspace

조회 수: 8 (최근 30일)
Alberto
Alberto 2019년 3월 14일
답변: Dennis 2019년 3월 14일
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

채택된 답변

Dennis
Dennis 2019년 3월 14일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by