How to name variable with file name?
Afficher commentaires plus anciens
Hi, i'm trying to make a code which create "mat" files from "cvs" automatically . I'm using an other program to to the conversion and i want to have a file and a variable automatically named. For example if i use "file1.csv" i want to get "file1.mat" which contain file1. I know how to name the file but not the variable.
Can someone help me or tell me a better way to do this please?
Here is my code:
for i=1:2;
m=txt2mat; % return the content of a specified file
save(['m_' num2str(i)],m);
end
1 commentaire
"How to name variable with file name?"
That is very buggy data design. Consider what happens if the file has one of these perfectly legal filenames:
1.csv
1-2.csv
@1.csv
quit.csv
a+b.csv
a(2).csv
Sure, you work on a limited set of filenames... then your colleague tries it with their files. Or in six months you forget this pointless filename restriction deep inside your code-base. Or you want to distribute your code and give other users a good impression. Why do you want to intentionally build latent bugs into your code?
"i want ... a variable automatically named"
Dynamically naming or accessing variable names is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code which is difficult to debug. Your .mat file data will be much easier to process when the variable names are the same in every .mat file.
"Can someone help me or tell me a better way to do this please?"
Simple: do not change the variable names.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur File Operations 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!