How to search for a index to a variable with known size
Show older comments
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 Comments
Stephen23
on 2 Nov 2023
So far you have not told us the most important information: how did you get those variables into the workspace?
Ludo Houben
on 3 Nov 2023
Edited: Ludo Houben
on 3 Nov 2023
"Thank you for your reply, but I see this reply to almost every message that I have been searching the last days."
That should be a big hint.
"I'm not asking about using the 'eval' function."
That is good, because if you think that my tutorial is about the EVAL function, then you have misunderstood it.
"I'm not talking about a FOR LOOP that is generating Var1, Var2, VarN variables."
That is also good, because my tutorial is not about "generating... variables" but is about accessing variable names dynamically.
"But to answer your question. I get the info from *.mat / *.m and *.csv files."
.MAT files you can always LOAD into an output variable and then acess its fields. This is explained in my tutorial.
For importing any other filetypes you can use whatever variable name you want, so should not be a problem.
"The tool is also creating a MATLAB *.m script that can convert the *.csv data into a MATLAB structure. However the user can freely choose the name of the main variable to generate. This is the point that I cannot control and I'm trying to find a solution for."
I agree that is most unfortunate data design. Sadly sometimes one has to work with such things.
Do you RUN that script in your code? Does it start with the FUNCTION keyword?
Note the difference: https://www.mathworks.com/help/matlab/matlab_prog/scripts-and-functions.html
Once you answer that, then most likely we can think about better ways to approach this task.
"However the user can also have saved previous 'manual' created *.mat files for the model to run and I want to function to coop also with this information."
I don't see why that should be a problem, just LOAD into an output variable and access its fields:
I appreciate your explanation. Unlike others I will not just answer your question:
but instead try to understand what is the actual goal and how it can be achieved. As the MATLAB documentation clearly states "Avoid functions that query the state of MATLAB such as inputname, which, whos, exist(var), and dbstack. Run-time introspection is computationally expensive."
If you are interested and want to learn more about how to write better MATLAB code then I am most happy to help you. Struggling with variable names or other kinds of programming introspection (e.g. relying on WHOS) is often solved very easily.
Ludo Houben
on 3 Nov 2023
Ludo Houben
on 3 Nov 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}
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
on 3 Nov 2023
Accepted Answer
More Answers (0)
Categories
Find more on Language Support in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
