Create a series of variables with names that include numbers

I need to create some fake data for testing a program. The fake data is a series of variables with names like VAR004300, VAR004400, VAR004500, etc. Each variable will be a 1x1 struct with 5 fields (same 5 fields for all variables). What is the smart way to create these variables? I would like to avoid typing out the field names the same way dozens of times. The dumb way:
VAR004300 = struct('FIELD1', value431, 'FIELD2', value432, ...);
VAR004400 = struct('FIELD1', value441, 'FIELD2', value442, ...)

3 Comments

Thanks, but as I commented in the accepted answer, I'm trying to create a test data sample with these variable names and I was looking for an efficient way to create them all without having to type them all in.
"I was looking for an efficient way to create them all without having to type them all in."
Sure, that was perfectly clear from your question. And as everyone here was explaining, there is no "efficient way" to do that. Not only that, forcing meta-data into variable names makes processing data much harder, slower, and buggier, so the whole approach should be avoided anyway. Which is exactly what the links you were given explain.
The simpler, easier, and much more efficient approach is to use one array. Exactly what kind of array is best depends on the specific situation.
If you really are interested in "efficient" ways of storing and working with data, then understand that meta-data is data, and that data should be stored in variables (not in variable names or fieldnames). Then you can really start to write neat, robust, efficient, generalizable code:

Sign in to comment.

 Accepted Answer

The funny thing is, what you want to do actually is the bad way to solve the problem.
Instead, learn to use arrays. Learn to use cell arrays, instead of dynamically naming your variables.

7 Comments

I get that. But I'm trying to efficiently create a test data set, and this is how the variables are named in the real data set. I cannot change the structure of the real data set. The test data needs to contain dozens of variables with names that only differ by 2 characters out of many.
How is your "test data set" represented? A MAT-file with a collection of variables, a (potentially large) collection of variables in the workspace, or something else?
It's a MAT-file with a collection of variables. The program I'm testing will load the MAT-file into the workspace and generate a .csv with a subset of the data in the MAT-file.
Then don't create them as "standalone" variables. Create them as fields of a base struct array then save the struct using the -struct option to write each field as a separate variable in the file. Then when you load the file with an output that will recreate that "base struct array".
cd(tempdir)
for k = 1:5
data.("x" + k) = (1:10).^k;
end
data
data = struct with fields:
x1: [1 2 3 4 5 6 7 8 9 10] x2: [1 4 9 16 25 36 49 64 81 100] x3: [1 8 27 64 125 216 343 512 729 1000] x4: [1 16 81 256 625 1296 2401 4096 6561 10000] x5: [1 32 243 1024 3125 7776 16807 32768 59049 100000]
save mymatfile.mat -struct data
whos -file mymatfile.mat
Name Size Bytes Class Attributes x1 1x10 80 double x2 1x10 80 double x3 1x10 80 double x4 1x10 80 double x5 1x10 80 double
newdata = load('mymatfile.mat')
newdata = struct with fields:
x1: [1 2 3 4 5 6 7 8 9 10] x2: [1 4 9 16 25 36 49 64 81 100] x3: [1 8 27 64 125 216 343 512 729 1000] x4: [1 16 81 256 625 1296 2401 4096 6561 10000] x5: [1 32 243 1024 3125 7776 16807 32768 59049 100000]
That's what I was looking for.
I guess you didn't check out the FAQ like in my answer because that was shown in the FAQ. Thankfully Steve answered soon or you would have gone longer without a solution. Anyway, the FAQ is still worth looking at because there is lots of interesting other info in there.
I actually did read it. I found a partial solution there, but Steven Lord's comment was more complete.

Sign in to comment.

Categories

Tags

Asked:

on 28 Apr 2023

Edited:

on 29 Apr 2023

Community Treasure Hunt

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

Start Hunting!