create variables' name in pattern

n0=imread('0.png')
n1=imread('1.png')
n2=imread('2.png')
n3=imread('3.png')
...
How can i create names in some patterns i prefer. And excute some non-number syntaxs like <imread('3.png')> in some patterns. Because '3' in '3.png' is not a number but a string, i cant use <for loop> for it.
I know someone may say and has said that you can use matrix.
This creats a 4-D matrix, because image itself is 3-D, and you need an additional dimension to indicate "where we put it", and it can use <for loop> due to indicators in 4-D matrix N( , , ,1) are numbers.
Creating 4-D matrix make the whole thing felt wierd and complicate. And you need to be sure the size for each image is the same so they can be fixed in 4-D matrix you create.
So far , how to name it is somehow tackled by this 4-D matrix, but what about right side <imread('3.png')>? how ?
And i dont like the method mentioned.

 Accepted Answer

If your files may vary in size/depth/class, use a loop to read them into a cell array instead.
If you create a bunch of named variables with indexing information embedded in the names, you've only created a new problem. You would then need a way to index the new names themselves, and there's no good reason to think that would be less complicated.
I understand that extending arrays into higher dimensions can become cumbersome to mentally visualize, but it doesn't take too long to get used to. As mentioned, using a cell array would avoid the need anyway.
Consider the example
filepattern = 'sources/a*.png'; % or whatever you're looking for
fileinfo = dir(filepattern);
numfiles = numel(fileinfo);
imagestack = cell(numfiles,1);
for f = 1:numfiles
fpath = fullfile(fileinfo(f).folder,fileinfo(f).name);
imagestack{f} = imread(fpath);
end
imshow(imagestack{1}) % show one

3 Comments

Caution: the above pattern might not read the files in what you would normally think of as numeric order. For example the file after a1.png would typically be a10.png . MATLAB returns files in whatever order the operating system returns. In turn, operating systems often return files in the way that file systems decide. File systems might be configurable.
For example the details of the order for an NTFS filesystem depend on which Locale was configured for the user who created the file system. I do not mean that the user had a direct choice. So for example with Canada being a bilingual country, if one of my coworkers happened to personally have set their interface to French then any NTFS they created might have a different sort order than if I had created it using exactly the same sequence of operations.
To circumvent this, you can either use sprintf to create the name based on your counter, or use sort to sort the names that dir returns (or get natsort from the file exchange).
jingwei chen
jingwei chen on 20 Feb 2022
Edited: jingwei chen on 20 Feb 2022
wow. You two are amazing. @DGM @Walter Roberson.
Although...Walter's one looks hardcore, i guess if i look deep down into it i have to start with Machine Language, Circuitry, Electrical Science, ..., Quantum Mechanics until the end of universe (joking). Thanks for helping, especailly by expert like you @Walter Roberson.
You solve my problem AND certainly get what i intend to do @DGM.
Quote yours : 'If you create a bunch of named variables with indexing information embedded in the names, you've only created a new problem. You would then need a way to index the new names themselves,'
Indeed, but if a software possesses capability of doing that no matter storing or extracting data at the very beginning , it wont be a problem.
i used other software before, which is able to do that by enabling f in <for loop> you wrote to be fixed in "everywhere", as a number ,as a string surfix ,as a string prefix, wherever . So when i start to learn matlab ,i think in old way.
thanks you two again.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 20 Feb 2022

Edited:

on 20 Feb 2022

Community Treasure Hunt

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

Start Hunting!