Changing file path in a loop and save figures

5 visualizaciones (últimos 30 días)
Ancalagon8
Ancalagon8 el 15 de Oct. de 2022
Comentada: Jan el 10 de Nov. de 2022
I want to run a code where inside every folder i have a data file and save in each folder a .fig. Can anyone help me?
  17 comentarios
Rik
Rik el 9 de Nov. de 2022
Editing away your question is considered rude. You have just decided that the help you received should not benefit others. What give you the right to make that decision?
You must not have been aware of these implication, but you can easily revert the question back to the original state and never do something like this in the future.
Jan
Jan el 10 de Nov. de 2022
@Ancalagon8: Please do not delete essential parts of the question. Afterwards the time spent for writing the answers is wasted. If all users do this, this forum would be useless. Therefore your "cleaning" is counter-productive and will reduce the interest to help you in the future.

Iniciar sesión para comentar.

Respuestas (2)

Simon Chan
Simon Chan el 15 de Oct. de 2022
Try this to indicate the entire path:
savefig(fullfile(thisDir,'DN.fig'))
  5 comentarios
Rik
Rik el 17 de Oct. de 2022
Why did you modify the code like that? The dir function and readtable functions do completely different things.
You should use dir to determine the name of your file, and then provide readtable with the full path and file name.
Stephen23
Stephen23 el 17 de Oct. de 2022
Editada: Stephen23 el 17 de Oct. de 2022
"I tried..."
Why did you stick DIR inside the loop like that? The entire point of DIR is to get a list of any files that match the specified name, it will also automatically detect all folders. The code I gave you replaces all of your (complex, non-working) code by using DIR more effectively.
"There is a typo in Stephens suggestion:"
No, I specifically did not want a recursive search. One star will match any subfolder, matching the OPs description.

Iniciar sesión para comentar.


Stephen23
Stephen23 el 17 de Oct. de 2022
Editada: Stephen23 el 18 de Oct. de 2022
As I wrote earlier, you need to get DIR to more of the work for you. It is simpler and much more efficient when you let DIR do as much as possible matching names and folders. I will demonstrate this here on the forum, but you should be able to adapt this to your own files.
First lets create some fake data files in subfolders:
for k = 'A':'D' % DO NOT COPY
mkdir(k) % DO NOT COPY
writematrix(rand(3,3),fullfile(k,'test.txt')) % DO NOT COPY
end % DO NOT COPY
clearvars % DO NOT COPY
Now lets use DIR to get a structure array of those files in all subfolders:
P = '.'; % absolute/relative path to where the subfolders are.
S = dir(fullfile(P,'*','test.txt')); % one line is much simpler than your code.
Note how a single asterisk is sufficient to match subfolders (without recursion). We can check what DIR found:
{S.name} % view check the filenames % DO NOT COPY
ans = 1×4 cell array
{'test.txt'} {'test.txt'} {'test.txt'} {'test.txt'}
{S.folder} % view the corresponding subfolder names % DO NOT COPY
ans = 1×4 cell array
{'/users/mss.system.T3k1jK/A'} {'/users/mss.system.T3k1jK/B'} {'/users/mss.system.T3k1jK/C'} {'/users/mss.system.T3k1jK/D'}
And then all you need to do is loop over those files:
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
... do whatever with this filename
end
  8 comentarios
Stephen23
Stephen23 el 18 de Oct. de 2022
Editada: Stephen23 el 18 de Oct. de 2022
tmp = load('k.mat')
tmp = struct with fields:
k: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 … ]
tmp.k
ans = 1×365
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
There is no obvious way that you would get k = 1x365 vector with the code that I gave you: the fact that k is a vector tells us that you are doing something different from what I showed you. Perhaps you did not write the FOR loop correctly... but without seeing your actual code that is just a guess.
If you want further help, please post the exact and unmodified code that you are using (by uploading if it is large) and also upload a mat file containing all workspace variables after the error occurs.
Rik
Rik el 24 de Oct. de 2022
I don't see what confidential information might be encoded in the path. You published it yourself under a creative commons license when you posted the mat file. I have therefore removed your flag.

Iniciar sesión para comentar.

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by