Use data array with specific names

조회 수: 6 (최근 30일)
Sam Hurrell
Sam Hurrell 2023년 1월 29일
편집: Dyuman Joshi 2023년 1월 29일
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