Use data array with specific names
Afficher commentaires plus anciens
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
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.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Variables 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!