Use fprintf name of array within the script

3 vues (au cours des 30 derniers jours)
Sam Hurrell
Sam Hurrell le 12 Juil 2022
I am trying to find the sizes of many arrays, named "d25a%d" for %d values of 16:1:20. I'm using a for loop with f equal to these values, the first line is forming the name of the file "file = 'd25a%d'; File = sprintf(file,f);" and then finding the size for the array with the same name as File, in the form of "[a,z] = size(File);" and the result is the size of the character array.
How can this be fixed?
  1 commentaire
Stephen23
Stephen23 le 12 Juil 2022
Modifié(e) : Stephen23 le 12 Juil 2022
"How can this be fixed?"
By not forcing meta-data into variable names.
You forgot to tell us the most important information: how did those variables get into the workspace? I doubt that you wrote all of those variable names out by hand.... e.g. if you used LOAD, then that would be the place to fix your code:
S = load(..);

Connectez-vous pour commenter.

Réponses (2)

Jan
Jan le 12 Juil 2022
Modifié(e) : Jan le 12 Juil 2022
The main problem is to have a bunch of variables with an index hidden in the name. Don't do this. Store the data in an array. Then it is trivial to run a loop to get the sizes.
  2 commentaires
Sam Hurrell
Sam Hurrell le 12 Juil 2022
I'm aware of this issue and later in the script resolves this. However indexing the variables at this stage is necessary and cannot easily be altered.
Jan
Jan le 12 Juil 2022
If you have a complicated representation, which requires even more complicated methods to process them, do not imporve these methods, but the representation of the data. Of course you can convert the data to an array and use standard indices:
Array = {d25a16, d25a17, d25a18, d25a19, d25a20};
Why do you use the term "file"? Do you have to import data from files at first?

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 12 Juil 2022
Don't worry - it's not at all uncommon to have parameters encoded into the file names. It appears you're trying to create 5 file names, from d25a16.* to d25a20.*, and read in their contents into variables. You can use the FAQ to read them in
or try this code (untested):
for k = 16 : 20
% Create filename.
baseFileName = sprintf('d25a%d.dat', k);
fullFileName = fullfile(folder, baseFileName);
% If the file exists, read it in.
if isfile(fullFileName)
% Read in the matrix and get its size.
data = readmatrix(fullFileName);
[rows, columns] = size(data);
fprintf('%s has %d rows and %d columns.\n', baseFileName, rows, columns);
else
% File does not exist.
warningMessage = sprintf('File not found:\n %s', fullFileName);
fprintf('%s\n', warningMessage);
uiwait(warndlg(warningMessage));
end
end
If you simply have 5 matrices in your program and want their sizes, simply use size():
[rows(1), columns(1)] = size(d25a16);
[rows(2), columns(2)] = size(d25a17);
[rows(3), columns(3)] = size(d25a18);
[rows(4), columns(4)] = size(d25a19);
[rows(5), columns(5)] = size(d25a20);

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by