Elegant way to batch extract all as single data from Data Pool
Afficher commentaires plus anciens
I am Matlab beginner, which to know if there's a quick way of indexing/slicing data into individual variable.
dataPool=magic(5)
Naive method is
p=dataPool(1,1);
q=dataPool(1,2);
r=dataPool(1,3);
s=dataPool(1,4);
t=dataPool(1,5);
Can I do a batch processing on this? For instance:
[p,q,r]=dataPool(1,:)
I pretty sure there must be some methods published in Mathworks, but it is hard to describe this problem as search string/keyword to look for help. Let me know if you know one webpage that talk about this. Your help is very much appreciated.
Thank you.
More information:
I am developing an algorithm that produces input matrix which composed of m*n (m runs* n parameter). Matrix example:
___________________________________________________
| Parameter1 Parameter2 Parameter3
____________________________________________________
Run#1 | 3 grams 45 degreeCelcius 3 bar Pressure
Run#2 | 4 grams 30 degreeCelcius 4 bar Pressure
These matrix will be extracted row-by-row to run a chemistry experiment, and the experiment program reads by variables' name (because these variables will be processed individually for instrument controller to start functioning), so each parameter must have a unique tag/variable_name. I am looking for a way, that helps to reduce my line of codes (i.e. Parameter1=dataPool(:,1);Parameter2=dataPool(:,2);) ,because i have long list of parameters
The convenient method is definitely the simplest way out. Just out of curiousity, if anything else works.
2 commentaires
Stephen23
le 4 Sep 2022
" if there's a quick way of indexing/slicing data into individual variable."
Indexing.
"i.e. Parameter1=dataPool(:,1);Parameter2=dataPool(:,2);)"
Numbering variable names (or more generally, forcing meta-data into variable names) is a sign that you are doing something wrong.
"so each parameter must have a unique tag/variable_name."
Variable names should not matter. Much better data design would for example store all of the meta-data and data in one table, making it trivial to access (and allowing you to really simplify and generalize your code).
@Stephen23's comment +inf
Reinforces what I've been preaching...
Réponse acceptée
Plus de réponses (1)
The real answer in MATLAB is "Don't Do That!" -- making variables out of array elements generally is not the way to write efficient MATLAB code and utilize its primary reason for being -- namely, the vectorized functionality.
This is a hard concept for those who come to MATLAB from other procedural languages, granted, but is key to making effective use of MATLAB.
Use array indexing instead...
Granted, occasionally it is convenient to address array segments as variables for convenience such as in your above example
x=dataPool(:,1);
y=dataPool(:,2:end);
plot(x,y)
lets you write a shorthand for the plot() function or other operations on the independent variable and its corollary dependent ones. Almost all MATLAB builtin function operate by column by default so the above plots the four columns against the first. Taking advantage of this is the key to effective and efficient MATLAB code both from the coding side as well as then performance when running.
What would be the end purpose of creating all these named variables that prompted the Q? Given a use case, we can probably illustrate "the MATLAB way" instead...
ADDENDUM
NOTA BENE: If x,y above are not manipulated numerically, then behind the scenes MATLAB will NOT create physical copies of the data but only references to the original. Hence, there is not a penalty for the coding convenience in this case.
Where the problem arises is that one you have created your above named variables, then you have no recourse going forward to use them but to use those specific names -- any generality is lost except by the arcane and performance-killing use of the not-to-be-mentioned-in-polite-company eval. And, you definitely do NOT want to even think about going down that route.
4 commentaires
Cha Yong Jong
le 3 Sep 2022
Would need the details of just how you interact with the controller, but I'd build the database as a MATLAB table instead of arrays; each column can hold the various parameter value for each set of test conditions, the first column would then be variable "Run" and the value 1:N (or, that can be implied if it is sequential but you may want to randomize run sequence for control of experimental variance).
Then, to run the sequence of experiments, simply iterate over the height() of the table and return/submit each data set in turn.
PS. Do NOT store metadata like units with the data values itself; the table actually has a units property you can associate to each; treat data as data and never the twain mix.
dpb
le 3 Sep 2022
"... developing an algorithm that produces input matrix ... to run a chemistry experiment,"
If you have the Statistics Toolbox, it contains a pretty good selection of experimental design tools D(esign)O(f)E(xperiments) or DOE that might possibly be of interest/help in setting up the design matrix.
Includes full/fractional factorials, RMS (response surface model-specific), various D-Optimal designs, etc., etc., ...
Cha Yong Jong
le 4 Sep 2022
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!