How to convert a string into a variable name and assign it a cell of data?
Afficher commentaires plus anciens
I've read multiple post: converting a string into a variable name seems quite problematic, but in my case convenient.
It's about to load multiple pictures from a folder.
But in order to not get confused with all the images, I want to assign the image name right to its data.
Here's my code, that just serves for loading the images:
clear
%% Load Picture Infos
Folder = '/Users/niklaskurz/Downloads/Bilderauswahl/*.jpg';
PictureLinks = imageDatastore(Folder);
PictureNames = dir(Folder);
PictureEdits = cell(2,1);
i = 1;
while hasdata(PictureLinks)
PictureEdits{1,i} = PictureNames(i).name;
PictureEdits{2,i} = read(PictureLinks);
i = i + 1;
end
This creates a cell with PictureNames in the first row and the data in the second. However it would be incredible having the name of the picture to be assigned to its data straight away, so that it can be called.
5 commentaires
"However it would be incredible having the name of the picture to be assigned to its data straight away, so that it can be called."
A function can be called, an array cannot be called. So you are already mixing up concepts, which does not bode well...
"But in order to not get confused with all the images, I want to assign the image name right to its data. "
The easiest way to do that is to use the structure array that you already have to store all of the imported data:
P = '/Users/niklaskurz/Downloads/Bilderauswahl';
S = dir(fullfile(P,'*.jpg'));
for k = 1:numel(S)
F = fullfile(P,S(K).name);
S(k).data = imread(F);
end
But really the elephant in the room is, why are you attempting to (mis-)use an (IMAGE)DATASTORE like that? The entire idea of an (IMAGE)DATASTORE is that it lets you refer to image data without needing to import it all into MATLAB memory. And yet all you seem to be using it for, is to avoid calling (much better suited) IMREAD when importing all of the image data into MATLAB's memory. Why mix everything up like that?
This code (and this question) has a very strong https://en.wikipedia.org/wiki/Code_smell
Jiri Hajek
le 6 Déc 2022
Hi, actually it seems you have taken the best path after all. There's really no good way to "...assign the image name right to its data.". You can indeed find a multitude of posts about how much better it is to use strutrure array with fieldnames being your desired variable names, and rightly so...
Stephen23
le 6 Déc 2022
"You can indeed find a multitude of posts about how much better it is to use strutrure array with fieldnames being your desired variable names"
I would not recommend doing that:
- it is more complex than simply using indexing into a cell array or structure array.
- It is extremely fragile: any filename that is not a valid MATLAB fieldname will throw an error.
Niklas Kurz
le 6 Déc 2022
Modifié(e) : Niklas Kurz
le 6 Déc 2022
"Maybe an improvement for Matlab would be setting a stable Function, that extracts the File name of an image and assigns it to its image data, since this is a practical way of remembering what is what."
I already showed you a very simple way of keeping the filenames and imported data together, using the structure array that you already have. The approach I showed you is simpler, neater, and much more efficient than what you are trying to do.
Given how TMW is actively moving away from (and discouraging) the complex, inefficient, anti-pattern paradigm that you prefer, such a function is very unlikely. Not only that, given that many characters are valid in filenames (varying by OS) but not in variable names, such a function would require a complex set of rules for modifying the names and avoiding namespace clashes. It would not be simpler, it would be much more complex than you think: usually when beginners make suggestions like this, they are thinking only about their specific scenario, and they forget that TMW actually writes MATLAB to work for all scenarios for all filenames for all locales for all OSs for all users. Indexing works in exactly the same way for all of those, your concept does not.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Standard File Formats dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!