How may I dynamically refer to a loaded mat array to modify or save it
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to write a simple piece of code where I provide a function with a list name (as a string) and a new string to add to the list (also a string). It will then load that library, add it to the list, sort it, and re-save it. I seem to be getting stuck how to dynamically reference the list name. I desire to do this so that I may add new entries to many other lists I have on my computer quickly with the console.
function addtolist(list,string)
load([list '.mat']); %%%this line works fine
list = vertcat([list '.mat'],string); %%%this line is incorrect
list = sort(list);
save([list '.mat']); %%%this line also incorrectly references the cultures list
end
INPUT: addtolist('Cultures','Armenian') OUTPUT: Nothing (old file is overwritten with line of data)
Thanks in advance for any help here,
William
0 commentaires
Réponses (3)
Walter Roberson
le 11 Déc 2015
Modifié(e) : Walter Roberson
le 11 Déc 2015
In particular use
filename = [list '.mat']
data = load(filename);
data.(list) = sort([data.(list); {string}]);
save(filename, 'data', '-struct');
I explicitly used cell array form as vertcat() of a char array does not work if the new item is not the same width but there is no problem with cell arrays.
0 commentaires
Voir également
Catégories
En savoir plus sur Cell Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!