フィルターのクリア

How to get values of selected variables from workspace?

13 ビュー (過去 30 日間)
Tejaswini Ramesh
Tejaswini Ramesh 2016 年 6 月 29 日
コメント済み: Sachin Nag 2018 年 2 月 23 日
I have a huge number of signals/variables imported into workspace and I require only some selected variables along with values. Every variable is a double array. I have used string compare to get the selected variables but this gives only the names of them but not the values. I have used who which gives me only the names of the variable. Please guide me on how to get the values of each of these variables in a double array format.
load Test_2_4_1.mat;
variables = who;
%h = workspaceHandle;
%V = GetVariable(h,'variables','workspace');
%%to compare n parts of variable names (strings)
variable_list = variables;
ans = strncmpi(variable_list,'PEC',3);
ans_double = +ans;
%k = 1;
i=1;
for k = 1:size(ans_double,1)
if ans_double(k) ==1
selected_signals(i) = (variable_list(k));
%value(i) = workspaceHandle.getVariable(selected_signals(i));
i=i+1;
end
end
selected = selected_signals.';
z = evalin('base','selected');

採用された回答

Guillaume
Guillaume 2016 年 6 月 29 日
編集済み: Guillaume 2016 年 6 月 29 日
The simplest way is to load your variable into a structure, simply by giving an output to load, then you can use dynamic field names to access your variables:
matcontent = load Test_2_4_1.mat;
usefield = strncmpi(fieldnames(matcontent),'PEC',3);
%instead of a loop you can then convert the structure into a cell array and simply index the cell array:
matcontent = struct2cell(matcontent);
selectedvariables = matcontent(usefield); %cell array of selected variables
Note:
  • do not use ans as a variable name. This is a name reserved by matlab. The content of ans can change at any time (any time you don't provide an output to a function).
  • avoid eval, evalin, etc. like the plague.
  • if x == 1 when x is logical (as in your case) is the same as if x. Converting x to double and then comparing to 1 is a complete waste of time.
  3 件のコメント
Guillaume
Guillaume 2016 年 6 月 29 日
Before converting the structure to a cell array:
allvars = fieldnames(matcontent);
selectednames = allvars(usefield); %after usefield is created obviously.
Sachin Nag
Sachin Nag 2018 年 2 月 23 日
Thanks a lot Guillaume. This was a very simple and helpful answer. Easy to understand and works!!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by