필터 지우기
필터 지우기

Use fprintf name of array within the script

조회 수: 1 (최근 30일)
Sam Hurrell
Sam Hurrell 2022년 7월 12일
답변: Image Analyst 2022년 7월 12일
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
Stephen23
Stephen23 2022년 7월 12일
편집: Stephen23 2022년 7월 12일
"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(..);

댓글을 달려면 로그인하십시오.

답변 (2개)

Jan
Jan 2022년 7월 12일
편집: Jan 2022년 7월 12일
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
Sam Hurrell
Sam Hurrell 2022년 7월 12일
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 2022년 7월 12일
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?

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2022년 7월 12일
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);

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by