Load and edit multiple poorly named .m files

Hello,
I don't really know how to write this question properly. I've spent the past few hours googling and I did not find a good solution. Most answers are "don't do this" - but let me explain.
I've got (not made by me!) a folder with maany subfolders and in each subfolder there are 4 different .m files - each file is a single (quite large, around 500x500) matrix.
All of the files follow some naming logic, but they are terrible to use in MATLAB.
See picture:
In the end I need to load each .m file, cut the size, and combine into a single matrix. I know how to cut the size and combine, but I don't know how to automate the import.
So what I want:
...to add this file as an input to the classification learner, which only accepts one single matrix?
The matrix needs to be in a specific order, which is sadly not alphabetic - but the order the folders were created in - Date.
I loaded all .m files up in the workspace and created a struct, but the order of the entries is wrong.
I would be happy if I just get this done, with whatever "not reccomended" or unrobust or ugly way.
I'm fine with:
  • the idea of using placeholders in a for-loop for the multiple variables - is this possible?
  • importing all files and renaming them 1:1:length(x) and then edit them in a loop - how?
How would you go about this problem?

댓글 수: 5

You can get the filenames (using the dir fx, then extracting from the name field) and replace them with ordered names in a for loop. Place the load fx and matrix processes within the for loop.
Marino
Marino 2021년 1월 6일
Thank you for your answer.
Will this only rename the file in the system (like windows folder), but not the name of the variable itself in the workspace?
?? I do not see any .m files in your listing? I see .mat files .
Marino
Marino 2021년 1월 6일
Sorry my bad, .mat files.
Stephen23
Stephen23 2021년 1월 6일
편집: Stephen23 2021년 1월 6일
"How would you go about this problem?"
I don't see why you need to rename any files or use "placeholders in a for-loop for the multiple variables" (whatever that means). Why not do the obvious:
  • Get the folder creation date (search this forum to know how).
  • Sort the folders into that order
  • process each folder (dir, load into output variables, etc.)
'Most answers are "don't do this"'
I have never seen an answer that says "do not process multiple folders/files in a loop". This is a very common task.
PS: none of your screenshots have any .m files in them, I can only see .mat files.

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

답변 (1개)

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

질문:

2021년 1월 6일

편집:

2021년 1월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by