How to search for a index to a variable with known size
4 ビュー (過去 30 日間)
古いコメントを表示
Hi All
I'm still relative new to MATLAB and I'm struggeling a bit to find an index. Following info I have received from a 'whos' command.
Now I want to 'find' the entry 'simTimeData' by searching the array based on the size, so something like:
index = find({variableList.size}==[226411,1])
index = 12
For me the size of the variable will be known, but it's name can change. Resulting furthermore in an unknown position in the array. That is why I want to 'search' it based on the size. Thanks for any suggestion.
With kind regards
Ludo Houben
7 件のコメント
Stephen23
2023 年 11 月 3 日
編集済み: Stephen23
2023 年 11 月 3 日
"Thats correct loading is working, but what variable to look for if you don't know the name?"
Once you LOAD into an output variable like this:
S = load(..);
then you can trivially use a loop (using FIELDNAMES and dynamic fieldnames) or STRUCTFUN (or CELLFUN after converting to cell array, etc).
Lets try it right now.
% create fake data in MAT file:
a = rand(2,3,4);
x = 4;
simTimeData = zeros(226411,1);
save test.mat
clearvars
% LOAD data:
S = load('test.mat');
% use STRUCTFUN to select the array with a particular size:
V = [226411,1]; % required size
F = @(a) isequal(size(a),V); % @(a) isscalar(a) && isstruct(a);
X = structfun(F,S);
C = struct2cell(S);
sdt = C{X}
And there is the desired array, without knowing anything about its name and without messing around with ugly introspective coding (WHOS, EVAL, etc). It also makes it much easier to add code checking (which you should be doing, e.g. checking how many arrays match the requested conditions, etc).
If you really want to know the array's name then use FIELDNAMES and the index X.
"Correct, but still the same issue of not knowing how the variable is called in the imported data. "
This statement I do not understand. When importing from e.g. CSV file you always use your variable names in your code.
ThisVariableYouNameYourself = readmatrix(..);
採用された回答
Voss
2023 年 11 月 2 日
% some variables:
a = rand(2,3,4);
x = 4;
simTimeData = zeros(226411,1);
% size to look for:
size_to_find = [226411,1];
% find it:
S = whos();
idx = find(cellfun(@(x)isequal(x,size_to_find),{S.size}))
S(idx)
7 件のコメント
Stephen23
2023 年 11 月 3 日
"For *.mat files ...The desired variable were created in the past and the workspace has a lot of unused variables. Or it can already contain the desired variables. This is the part where I'm currently working on."
LOAD into an output variable, then access its fields:
S = load(..);
"For *.csv files (Export of the external tool) simSignalData = eval(variableList(idx).name); % I could not find a solution without the eval() yet. The desired variable was created by a earlier runned script (export from the external tool)."
It is unclear why WHOS and EVAL would be required, you can import that CSV file data directly into a variable with a fixed name.
"For *.m files (Files created in the new 'way of working' and/or manually created with data for unit tests)... The desired variables are already present and can be directly used. For all files I will then create the following variables in the base workspace:"
Best avoided (see my tutorial). Much better: write a function and pass them as output arguments:
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Data Type Identification についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!