Use data array with specific names

5 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2023-1-29
"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.

请先登录,再进行评论。

回答(1 个)

the cyclist
the cyclist 2023-1-29
编辑:the cyclist 2023-1-29
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 个评论
Stephen23
Stephen23 2023-1-29
"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 2023-1-29
编辑:Dyuman Joshi 2023-1-29
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?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by