How to search for a index to a variable with known size

5 visualizaciones (últimos 30 días)
Ludo Houben
Ludo Houben el 2 de Nov. de 2023
Comentada: Ludo Houben el 3 de Nov. de 2023
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 comentarios
Stephen23
Stephen23 el 3 de Nov. de 2023
Editada: Stephen23 el 3 de Nov. de 2023
"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}
sdt = 226411×1
0 0 0 0 0 0 0 0 0 0
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(..);
Ludo Houben
Ludo Houben el 3 de Nov. de 2023
Hi Stephen23
Thanks for your code example. I'm going 'to play' with this solution.
Regarding to your comment:
"This statement I do not understand. When importing from e.g. CSV file you always use your variable names in your code.
ThisVariableYouNameYourself = readmatrix(..);"
'ThisVariableYouNameYourself' is not generating the needed overall structure. In the CSV file is NO variable naming and NO variable type information available. That's why I have to run the external created script.
Cheers
Ludo

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 2 de Nov. de 2023
% 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}))
idx = 3
S(idx)
ans = struct with fields:
name: 'simTimeData' size: [226411 1] bytes: 1811288 class: 'double' global: 0 sparse: 0 complex: 0 nesting: [1×1 struct] persistent: 0
  7 comentarios
Stephen23
Stephen23 el 3 de Nov. de 2023
"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:
Ludo Houben
Ludo Houben el 3 de Nov. de 2023
"LOAD into an output variable, then access its fields:
S = load(..);"
I'm doing this, but I don't know the field name....that's the root cause of my challenge.
"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."
The CSV file contains only the data, not the variable name and variable type information. So with only the CSV file I cannot create the required structure. At this time the external tool creates a script that holds this info, but I do not know the name of the variable that is used (See https://nl.mathworks.com/matlabcentral/answers/2041991-how-to-search-for-a-index-to-a-variable-with-known-size#comment_2947251 for more info)
"Best avoided (see my tutorial). Much better: write a function and pass them as output arguments:
This is what I'm trying to create :-)
Cheers
Ludo

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by