Use data array with specific names

2 vues (au cours des 30 derniers jours)
Sam Hurrell
Sam Hurrell le 29 Jan 2023
Modifié(e) : Dyuman Joshi le 29 Jan 2023
I have multiple data arrays imported to the workspace with similar names that differ only by number (eg. data15, data20,...data90). I want to combine them into a single data array called 'File' so that I can use them in other scripts. What I'm having to do at the moment is manually input the command (File(:,1:100) = data15, File(:,101:200) = data20, etc.), but what I want to do is have limiting factors (15:5:90) in a script that'll sequentially load them into the File array. I tried doing this via sprintf but that only created chars that I can't use.
How can this be done?
  1 commentaire
Stephen23
Stephen23 le 29 Jan 2023
"I have multiple data arrays imported to the workspace with similar names that differ only by number (eg. data15, data20,...data90)."
Bad data design is the cause of your difficulties:
  • numbered variable names are a sign that you are doing something wrong.
  • forcing meta-data (e.g. pseudo-indices) into variable names is a sign that you are doing something wrong.
Once you have lots of numbered variables like that you force yourself into writing sow, complex, inefficient, obfuscated, buggy code to just perform the basic task of accessing your data. Ugh. Read this to know some of the reasons why:
"How can this be done?"
So far you have not told us the most important information: how did you get all of those variables into the workspace? I doubt that you sat a wrote them all out my hand, so most likely they were created somehow: that is the correct place to fix your code. For example, instead of LOADing directly into the workspace, you should always LOAD into an output variable:
S = load(..)
after which STRUCT2CELL(), FIELDNAMES(), and/or dynamic fieldnames may be very useful:
Your approach should be avoided.

Connectez-vous pour commenter.

Réponses (1)

the cyclist
the cyclist le 29 Jan 2023
Modifié(e) : the cyclist le 29 Jan 2023
You are seeing first-hand why variables should not be named dynamically. If at all possible, this problem should be solved upstream.
But, if you have no such option, you can use the eval command to do this. Here is one way.
% Make up some data. (Use your real data here.)
data15 = rand(2,100);
data20 = rand(2,100);
data25 = rand(2,100);
% Define empty File
File = [];
% List of the dynamic data file numbers
filenumList = 15:5:25;
for fn = filenumList
eval(sprintf("File = [File data%d];",fn));
end
% Show that File is equivalent to stacking the individual data files
isequal(File,[data15 data20 data25])
ans = logical
1
I did not preallocate File and fill in each segment, as you had coded it. This will be important to do, for memory management, if File is large. I just got lazy in coding how that will go.
I cannot emphasize enough what a bad coding practice the above is. It should be avoided unless you are truly stuck with no othe rway.
  3 commentaires
Stephen23
Stephen23 le 29 Jan 2023
"How does this approach compare to using eval?"
Much more efficient to run, much easier to debug, possibly more typing is required.
Dyuman Joshi
Dyuman Joshi le 29 Jan 2023
Modifié(e) : Dyuman Joshi le 29 Jan 2023
I had an inkling towards it, Thanks for clarifying it!
I tagged you in a comment to another question Stephen, could you please look at it?

Connectez-vous pour commenter.

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by