- Is there really a space in data _thesis?
- Which of these folders are in the search path of Matlab?
- Are these m-files scripts of functions? If functions, do they have input and/or output arguments?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
auto run and delete files from multiple subfolders
3 views (last 30 days)
Show older comments
My question consist over three parts
Part 1
My current directory is
E:\meta data\new\data _thesis\NCEP\winds_speed
In winds_speed , there are 30 folders (for year) and inside of each of these 30 folders there are further 12 folders are present.
Inside each of 12 folders, there is sorted_lat_split_NCAR_NCEP.m which i want to run automatically?
Part 2:
In this directory
E:\meta data\new\data _thesis\NCEP\winds_speed
there are 30 folders and inside of each of these 30 folders there are 12 folders and inside of each 12 folder, there is a runmyfile.m file which i want to delete from each folder?
Part 3
In this directory
E:\meta data\new\data _thesis\NCEP\winds_speed
there are 30 folders and inside of each of these 30 folders there are 12 folders and inside of each 12 folder, there is a abc.txt file inside of each of 12 folder. I want to paste each abc.txt file from this folder to my current directory
I am on window 7 and using matlab 2013, how can this do??
7 Comments
per isakson
on 12 Apr 2016
Edited: per isakson
on 12 Apr 2016
Muhammad Usman Saleem
on 12 Apr 2016
Edited: Muhammad Usman Saleem
on 12 Apr 2016
(1) yes , there is space in data _thesis
(2) in my mentioned current directory, there are 30 folders, and inside of each of these 30 folders, there are further 12 folder. My .m file which i want to run, is inside in each of these 12 folder.
(3) No , i want only scripts which able to perform my requirements.
Muhammad Usman Saleem
on 12 Apr 2016
@per isakson for explanation of your second point
At directory address
E:\meta data\new\data _thesis\NCEP\winds_speed
Let assume that these are my folders, when i click on winds_speed folder, further folders are as below
1985 ==> apr ==>sorted_lat_split_NCAR_NCEP.m
1986 aug ==>sorted_lat_split_NCAR_NCEP.m
1987 dec==>sorted_lat_split_NCAR_NCEP.m
1989 feb==>sorted_lat_split_NCAR_NCEP.m
1990 jan==>sorted_lat_split_NCAR_NCEP.m
1991 july ==>sorted_lat_split_NCAR_NCEP.m
1992 jun ==>sorted_lat_split_NCAR_NCEP.m
1993 marc ==>sorted_lat_split_NCAR_NCEP.m
1994 may==>sorted_lat_split_NCAR_NCEP.m
1995 nov==>sorted_lat_split_NCAR_NCEP.m
1996 oct==>sorted_lat_split_NCAR_NCEP.m
1997 sept==>sorted_lat_split_NCAR_NCEP.m
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
As you can look this, these are 30 folders with each folder contains 12 folders inside. My .m file which i want to run is inside of these 12 folders.
Muhammad Usman Saleem
on 14 Apr 2016
Part 3
Now after running each .m file in month folder. In each folder of month there are 24 text files name with Output_00.txt to Output_23.txt. I want to gather all text file of name Output_00.txt in
E:\meta data\new\data _thesis\NCEP\winds_speed
Such that , my code gather output_00.txt of apr folder in each year (1985 to 2015) and make a folder in my above mentioned directory with name apr.
This folder contains all apr output_00.txt file of all years. There will be 30 output_00.txt file in this folder
Tell me if i unable to explain my question please?
per isakson
on 15 Apr 2016
Edited: per isakson
on 15 Apr 2016
- "24 text files name with Output_00.txt to Output_23.txt"   The script, sorted_lat_split_NCAR_NCEP.m, which you uploaded creates 17 files named. output_0.txt, ..., output_16.txt. Note the single 0 in output_0.txt.
for x = 0 : 16
....
fId = fopen( sprintf( 'Output_%d.txt', x), 'w' ) ;
fprintf( fId, ' \t \t \r\n' ) ; % first line
fprintf( fId, '%.3f\t%.3f\t%.3f\r\n', data(:) ) ;
fclose( fId ) ;
end
- "gather output_00.txt of apr folder in each year (1985 to 2015)"   What do you exactly mean by gather? For instance, shall the the first line of each file be included? (Or is gather the name of the script, to be constructed?)
- What about the other output-files and the other months?
- The output-files don't contain any information on date and time
Muhammad Usman Saleem
on 15 Apr 2016
Edited: Muhammad Usman Saleem
on 15 Apr 2016
- I appologise about miss confusion. yes there are 17 text files with name Output_00.txt to Output_16.txt and my code just extract these files from orginal one.
- Not it i mean name of the script, to be constructed and first line of each file be included here are the meaning of Gather(what i want to explain
I am at this folder
E:\meta data\new\data _thesis\NCEP\winds_speed
In this folder there are 30 folder with name 1985 to 2015.
Alright..
Now in 1985 folder , address of the directory will be
E:\meta data\new\data _thesis\NCEP\winds_speed\1985
In this this folder of 1985, there are 12 more folder with name of apr, aug,...sep (For year 1985 , monthly temperature data in folder)
Lets move ahead or not?
Now after click on apr folder, address is
E:\meta data\new\data _thesis\NCEP\winds_speed\1985\apr
(Here we have auto run our mat file. Alright!)
Now here will be 17 text files name i mentioned above.
Below lines, will understandable if you understand my problem well.
With your previous code, we have run all month .m code from 1985 to 2015 in each month.
I want to copy(move) output_00.txt file of each month of each year into a folder with name Output_00.txt. So that when i open this folder, instead of moving in batch, i find all output_00.txt file in this folder?
I hope , i explain my best. If no, let me know please.
Thanks
per isakson
on 15 Apr 2016
Edited: per isakson
on 15 Apr 2016
- "with name Output_00.txt"   Did you change the script, , sorted_lat_split_NCAR_NCEP.m, to output double zero, 00?
- "my code just extracts these files from original one"   the word, "original one", is new to me. Does "extracts" refer to the operation of the script, sorted_lat_split_NCAR_NCEP.m?
"(Here we have auto run our mat file. Alright!)"   No, because
- I don't know what "auto run mat file" means and I failed to find it in the Matlab documentation. (I very recently updated from R2013a.)
- I wasn't aware that you use mat-files to solve this task
I believe, I understand the folder tree you use for this task.
E:\meta data\new\data _thesis\NCEP\winds_speed
1985
jan
...
dec
...
2015
jan
...
dec
"I want to copy(move) output_00.txt file of each month of each year into a folder with name Output_00.txt."
- New sub-folders will be added to the root.
- However, I don't understand the copy part, since one folder can only contain one file named, output_00.txt.
E:\meta data\new\data _thesis\NCEP\winds_speed
Output_00.txt
...
Output_16.txt
"So that when i open this folder, instead of moving in batch"   What is "moving in batch"?
Accepted Answer
per isakson
on 12 Apr 2016
Edited: per isakson
on 14 Apr 2016
I see two approaches.
Create cell arrays of strings containing the names of the sub-folders on the different levels. Use these cell arrays to loop over all folders. Use cd to make the folders the current folder and run the script. Pro: you have exact control over which folders you visit. Con: a lot of work.
Second approach: Use a function, which finds all files under one folder and which matches a given pattern. Search for rdir in the File Exchange or use cmd_where, which I attach. ( rdir is smarter, but I don't think you need that now.) cmd_where uses the dos-command where and thus it ignores the search path of Matlab.
sad_list = cmd_where( 'E:\meta data\new\data _thesis\NCEP\winds_speed' ...
, 'sorted_lat_split_NCAR_NCEP.m' );
%
for sad = sad_list
run( fullfile( sad.dir, sad.name ) )
end
Pro: little work. Con: cmd_where has problems with exotic characters in the filespec string; you might run scripts that you didn't intend to run; I haven't tested your case.
It might be a good idea to write a verbose report to a log-file. And add some assert.
Part 2.
sad_list = cmd_where( 'E:\meta data\new\data _thesis\NCEP\winds_speed' ...
, 'runmyfile.m' );
%
for sad = sad_list
delete( fullfile( sad.dir, sad.name ) )
end
Part 3. "paste each abc.txt file from this folder to my current directory"   I don't understand what you mean
28 Comments
Muhammad Usman Saleem
on 12 Apr 2016
Edited: per isakson
on 13 Apr 2016
@per , here is the output which i got from your code
>> sad_list = cmd_where( 'E:\meta data\new\data _thesis\NCEP\winds_speed' ...
, 'sorted_lat_split_NCAR_NCEP.m')
sad_list =
1x373 struct array with fields:
name
date
bytes
isdir
datenum
dir
when i try
>> for sad = sad_list
run( fullfile( sad.dir, sad.name ) )
end
Error using fileread (line 27)
Could not open file modified.New Text Document.txt. No such file or
directory.
Error in sorted_lat_split_NCAR_NCEP (line 5)
data = textscan( fileread( 'modified.New Text Document.txt' ), fSpec ) ;
Error in run (line 63)
evalin('caller', [script ';']);
Also see my code, which i have posted above in comments on @Image answer
per isakson
on 12 Apr 2016
Edited: per isakson
on 13 Apr 2016
sad_list =
1x373 struct array with fields:
is an expected result. It says that there are 373 files named sorted_lat_split_NCAR_NCEP.m
The errors occur in your script. I guess the reason is that your script must be run in the current directory. Try this
sad_list = cmd_where( 'E:\meta data\new\data _thesis\NCEP\winds_speed' ...
, 'sorted_lat_split_NCAR_NCEP.m' );
start_dir = cd();
for sad = sad_list
cd( sad.dir )
run( sad.name )
end
cd( start_dir )
PS: I seldom use scripts or relies on the current directory for anything but small quick tests. The code I first proposed would probably create a huge mess of variables in the original current directory, start_dir
PPS:   evalin('caller', [script ';']);   is that a line in a script?
Muhammad Usman Saleem
on 12 Apr 2016
Edited: per isakson
on 12 Apr 2016
list of error
>> sad_list = cmd_where( 'E:\meta data\new\data _thesis\NCEP\winds_speed' ...
, 'sorted_lat_split_NCAR_NCEP.m');
old_cd = cd();
for sad = sad_list
cd( sad.dir )
run( sad.name )
end
cd( old_cd )
Error using fileread (line 27)
Could not open file modified.New Text Document.txt. No such file or
directory.
Error in sorted_lat_split_NCAR_NCEP (line 5)
data = textscan( fileread( 'modified.New Text Document.txt' ), fSpec ) ;
Error in run (line 63)
evalin('caller', [script ';']);
per isakson
on 12 Apr 2016
Edited: per isakson
on 13 Apr 2016
Error in sorted_lat_split_NCAR_NCEP (line 5)
data = textscan( fileread( 'modified.New Text Document.txt' ), fSpec ) ;
No surprise this line causes an error. fileread takes one filename. 'modified.New Text Document.txt' is more like three filenames. What is the origin of that string? Windows accepts all kinds of weird filenames so I guess it's a legal Windows filename. However, it's asking for trouble to use it. If it's really the name of one file, where is that stored?
 
Error in run (line 63)
evalin('caller', [script ';']);
The error occurs in the function, run. I missed that.
per isakson
on 13 Apr 2016
Edited: per isakson
on 13 Apr 2016
- The output and error message, which you show in your comment to the answer by @Image, is most likely produced by the script, sorted_lat_split_NCAR_NCEP.
- I guess the reason is that your script must be run in the current directory.   Now, I guess I was wrong in my guess.
- Confirm that sorted_lat_split_NCAR_NCEP runs correctly when you start it interactively in the intended current directory.
- Does sorted_lat_split_NCAR_NCEP change the current directory?
Muhammad Usman Saleem
on 13 Apr 2016
Good morning @per Isakson
This is my reply to your points
(3) I have checked it manually sorted_lat_split_NCAR_NCEP.m runs correctly in interactively in the intended folder where i try to run it.(remember i run this script i have to give path to the folder)
(4) yes sorted_lat_split_NCAR_NCEP.m change the current director.
Muhammad Usman Saleem
on 13 Apr 2016
I have check my sorted_lat_split_NCAR_NCEP.m file. The name of the text file it read modified.New Text Document.txt which i correct.
I can not change name of this file because , i have manually change name of all text files in all folders.
Here i have attached my one of 'modified.New Text Document.txt' file and sorted_lat_split_NCAR_NCEP.m
Take a look of this please
per isakson
on 13 Apr 2016
- I downloaded your two files to h:\m\cssm, which is not in the search path of Matlab
- "(4) yes sorted_lat_split_NCAR_NCEP.m change the current director."   The attached sorted_lat_split_NCAR_NCEP.m does not change the current directory explicitly. However, the function, run temporarily sets the folder of the script as the current directory.
- The attached sorted_lat_split_NCAR_NCEP.m together with modified.New Text Document.txt in the same folder runs fine. The folder is not in the search path of Matlab. (Agrees with the documentation of run.)
- Remains the question why my code doesn't work on your system.
Test:
pwd
sad_list = cmd_where( 'h:\m\cssm', 'sorted_lat_*.m' );
for sad = sad_list
ffs = fullfile( sad.dir, sad.name )
run( ffs )
end
prints to the command window
ans =
c:\tmp
ffs =
h:\m\cssm\sorted_lat_split_NCAR_NCEP.m
Export for pressure ID 0 -> p=1000.0hpa.
Export for pressure ID 1 -> p=925.0hpa.
...
as expected
per isakson
on 13 Apr 2016
Edited: per isakson
on 13 Apr 2016
Run this test to confirm that all text-files exist
sad_list = cmd_where( 'E:\meta data\new\data _thesis\NCEP\winds_speed' ...
, 'sorted_lat_split_NCAR_NCEP.m' );
%
for sad = sad_list
ffs = fullfile( sad.dir, 'modified.New Text Document.txt' );
if exist( ffs, 'file' ) == 2
fprintf( 'Exists: "%s"\n', ffs )
else
fprintf( 2, 'Cannot find "%s"\n', ffs ) %#ok<PRTCAL>
end
end
Muhammad Usman Saleem
on 13 Apr 2016
Edited: Muhammad Usman Saleem
on 13 Apr 2016
It seem ,
sad_list = cmd_where( 'h:\m\cssm', 'sorted_lat_*.m' );
h:\m\cssm is the path, where my .m files are present, how can i manually give path as each times it change
Muhammad Usman Saleem
on 13 Apr 2016
Edited: per isakson
on 14 Apr 2016
output
Cannot find "E:\meta data\new\data _thesis\NCEP\winds_speed\modified.New Text Document.txt"
Exists: "E:\meta data\new\data _thesis\NCEP\winds_speed\1985\apr\modified.New Text Document.txt"
Exists: "E:\meta data\new\data _thesis\NCEP\winds_speed\1985\aug\modified.New Text Document.txt"
Exists: "E:\meta data\new\data _thesis\NCEP\winds_speed\1985\dec\modified.New Text Document.txt"
...
369 line starting with "Exists:" deleted.
The full text is attached, *|output.txt|*
In work space
ffs =='E:\meta data\new\data _thesis\NCEP\winds_speed\2015\sept\modified.New Text Document.txt'
sad== 1*1 str
sad_list= 1*373 str
per isakson
on 13 Apr 2016
Edited: per isakson
on 13 Apr 2016
Now I leave the computer for a cup of coffee. "Read my question again please"   No, you should be able to understand the result of the test I did. It's always possible to replace a constant string by a string variable.
If you think I missed something, tell me what.
Run the test, which I proposed in my previous comment.
Muhammad Usman Saleem
on 13 Apr 2016
Edited: Muhammad Usman Saleem
on 13 Apr 2016
Oh No;
this code is running all .m file in 1985 (first folder)
dir(pwd);
pwd
for ii=1:length(dir)
sad_list = cmd_where( 'E:\meta data\new\data _thesis\NCEP\winds_speed\1985', 'sorted_lat_split_NCAR_NCEP*.m' );
for sad = sad_list
ffs = fullfile( sad.dir, sad.name )
run( ffs )
end
end
I wounder to see, there is not any change in output even i comment out outer for loop
Muhammad Usman Saleem
on 13 Apr 2016
I am trying this but how can i give input to string?
pwd
myfolder={'E:\meta data\new\data _thesis\NCEP\winds_speed\1985','E:\meta data\new\data _thesis\NCEP\winds_speed\1986','E:\meta data\new\data _thesis\NCEP\winds_speed\1987','E:\meta data\new\data _thesis\NCEP\winds_speed\1988','E:\meta data\new\data _thesis\NCEP\winds_speed\1989','E:\meta data\new\data _thesis\NCEP\winds_speed\1990','E:\meta data\new\data _thesis\NCEP\winds_speed\1991','E:\meta data\new\data _thesis\NCEP\winds_speed\1992','E:\meta data\new\data _thesis\NCEP\winds_speed\1993','E:\meta data\new\data _thesis\NCEP\winds_speed\1994','E:\meta data\new\data _thesis\NCEP\winds_speed\1995','E:\meta data\new\data _thesis\NCEP\winds_speed\1996','E:\meta data\new\data _thesis\NCEP\winds_speed\1997','E:\meta data\new\data _thesis\NCEP\winds_speed\1998','E:\meta data\new\data _thesis\NCEP\winds_speed\1999','E:\meta data\new\data _thesis\NCEP\winds_speed\2000','E:\meta data\new\data _thesis\NCEP\winds_speed\2001','E:\meta data\new\data _thesis\NCEP\winds_speed\2002','E:\meta data\new\data _thesis\NCEP\winds_speed\2003','E:\meta data\new\data _thesis\NCEP\winds_speed\2004','E:\meta data\new\data _thesis\NCEP\winds_speed\2005','E:\meta data\new\data _thesis\NCEP\winds_speed\2006','E:\meta data\new\data _thesis\NCEP\winds_speed\2007','E:\meta data\new\data _thesis\NCEP\winds_speed\2008','E:\meta data\new\data _thesis\NCEP\winds_speed\2009','E:\meta data\new\data _thesis\NCEP\winds_speed\2010','E:\meta data\new\data _thesis\NCEP\winds_speed\2011','E:\meta data\new\data _thesis\NCEP\winds_speed\2012','E:\meta data\new\data _thesis\NCEP\winds_speed\2013','E:\meta data\new\data _thesis\NCEP\winds_speed\2014','E:\meta data\new\data _thesis\NCEP\winds_speed\2015'};
for ii=1:length(myfolder)
sad_list = cmd_where( E:\meta data\new\data _thesis\NCEP\winds_speed\1985, 'sorted_lat_split_NCAR_NCEP*.m' );
for sad = sad_list
ffs = fullfile( sad.dir, sad.name )
run( ffs )
end
per isakson
on 13 Apr 2016
Edited: per isakson
on 13 Apr 2016
The interesting part is (the only line that starts by "Cannot find")
Cannot find "E:\meta data\new\data _thesis\NCEP\winds_speed\modified.New Text Document.txt"
it tells us that the folder, E:\meta data\new\data _thesis\NCEP\winds_speed contains a file named sorted_lat_split_NCAR_NCEP.m, but no file named modified.New Text Document.txt. That explains the error we are seeing.
- You have 373 copies of sorted_lat_split_NCAR_NCEP.m under the root. Which of these do you want to run?
Muhammad Usman Saleem
on 13 Apr 2016
Edited: Muhammad Usman Saleem
on 13 Apr 2016
Please, here my sorted_lat_split_NCAR_NCEP.m and modified.New Text Document.txt exited which i want to run in this(apr) folder
E:\meta data\new\data _thesis\NCEP\winds_speed\1985\apr
where my current directory is
E:\meta data\new\data _thesis\NCEP\winds_speed
in wind_speed folder there are 30 folders name 1985 to 2015 and in each of the this folder(you can see in above directory 1985) there are 12 folders of month(let check of apr) , in each month folder there are my both two files of text and .m
Muhammad Usman Saleem
on 13 Apr 2016
Edited: per isakson
on 13 Apr 2016
I resolve the problem with this code
pwd;
myfolder={'E:\meta data\new\data _thesis\NCEP\winds_speed\1985' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\1986' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\1987' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\1988' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\1989' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\1990' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\1991' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\1992' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\1993' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\1994' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\1995' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\1996' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\1997' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\1998' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\1999' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2000' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2001' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2002' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2003' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2004' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2005' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2006' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2007' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2008' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2009' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2010' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2011' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2012' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2013' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2014' ...
, 'E:\meta data\new\data _thesis\NCEP\winds_speed\2015' };
for ii=1:length(myfolder)
folder=char(myfolder{ii});
% B=num2str(cell2mat(myfolder));
sad_list = cmd_where( folder, 'sorted_lat_split_NCAR_NCEP*.m' );
for sad = sad_list
ffs = fullfile( sad.dir, sad.name );
run( ffs )
end
end
Tell me whether this approach is correct or not?
per isakson
on 13 Apr 2016
Edited: per isakson
on 14 Apr 2016
I'm confused! There are 372 cases (month folders) (31 years times 12 month).
- Which of these do you want to run?
- Why do you mention the subfolders, apr
- Ok, this conversation has proceeded so quickly that I have missed one or two of your comments while testing and writing comments. Now, you have arrived at a solution and provided answers to all my question.
Muhammad Usman Saleem
on 13 Apr 2016
Edited: Muhammad Usman Saleem
on 13 Apr 2016
(1)As you have checked from my uploaded files, i have to give path to that folder where i save both txt file and mat file.Let this this folder has address E:\meta data\new\data _thesis\NCEP\winds_speed\1985\apr
(2) Now i have save cmd_where.m in folder which address E:\meta data\new\data _thesis\NCEP\winds_speed
(3) You can see address of the folder where mat and text file save comes after \1985\apr.
(4)In the directory 1985, there are 30 folders and in the month lets say \apr, there are 12 folders of month. Now we reach our both files address????? which i want to run in this same folder.
(5)This thing continue for every year. That why we got 31 years times 12 month (372 cases) which i want to run in their respective folder of month.( got my point or not??)
(6)I hope i reply to three points of your comment
Muhammad Usman Saleem
on 13 Apr 2016
Edited: Muhammad Usman Saleem
on 13 Apr 2016
Edit:
Are you got my question or i am unable to explain you please?
Where you do editing , i do not got please?
per isakson
on 13 Apr 2016
Edited: per isakson
on 14 Apr 2016
"whether this approach is correct or not?"   Yes, I think so. However, you could improve the code somewhat:
Clean up the outer loop a bit and remove the wildcard "*". It isn't needed and could pick up a file named for instance sorted_lat_split_NCAR_NCEP_old.m and thus cause confusion.
for folder = myfolder
sad_list = cmd_where( folder{:}, 'sorted_lat_split_NCAR_NCEP.m' );
for sad = sad_list
ffs = fullfile( sad.dir, sad.name );
run( ffs )
end
end
It is possible to use arrayfun to create myfolder  
frmt = 'E:\\meta data\\new\\data _thesis\\NCEP\\winds_speed\\%4d';
myfolder = arrayfun( @(yy) sprintf( frmt, yy), [1985:2015], 'uni',false );
Once the problem was spotted, the quickest fix might have been to rename (or move) the file,
e:\meta data\new\data _thesis\NCEP\winds_speed\sorted_lat_split_NCAR_NCEP.m
And the quickest way to spot the problem might have been to use
dbstop if error
and inspect the values of sad.dir and sad.name when the error was encountered
One more comment. The odd number, 273, in the output
sad_list =
1x373 struct array with fields:
name
date
bytes
isdir
datenum
dir
should have made us suspicious, since 12 month times a number of years gives an even number.
Muhammad Usman Saleem
on 14 Apr 2016
I would like to use previous approach,But your kind comment Yes, I think so. offering me in confusion, The beauty of this code is it show in command window where it run .m file, while the above lack this ability.
pwd;
myfolder={'E:\meta data\new\data _thesis\NCEP\winds_speed\1985','E:\meta data\new\data _thesis\NCEP\winds_speed\1986','E:\meta data\new\data _thesis\NCEP\winds_speed\1987','E:\meta data\new\data _thesis\NCEP\winds_speed\1988','E:\meta data\new\data _thesis\NCEP\winds_speed\1989','E:\meta data\new\data _thesis\NCEP\winds_speed\1990','E:\meta data\new\data _thesis\NCEP\winds_speed\1991','E:\meta data\new\data _thesis\NCEP\winds_speed\1992','E:\meta data\new\data _thesis\NCEP\winds_speed\1993','E:\meta data\new\data _thesis\NCEP\winds_speed\1994','E:\meta data\new\data _thesis\NCEP\winds_speed\1995','E:\meta data\new\data _thesis\NCEP\winds_speed\1996','E:\meta data\new\data _thesis\NCEP\winds_speed\1997','E:\meta data\new\data _thesis\NCEP\winds_speed\1998','E:\meta data\new\data _thesis\NCEP\winds_speed\1999','E:\meta data\new\data _thesis\NCEP\winds_speed\2000','E:\meta data\new\data _thesis\NCEP\winds_speed\2001','E:\meta data\new\data _thesis\NCEP\winds_speed\2002','E:\meta data\new\data _thesis\NCEP\winds_speed\2003','E:\meta data\new\data _thesis\NCEP\winds_speed\2004','E:\meta data\new\data _thesis\NCEP\winds_speed\2005','E:\meta data\new\data _thesis\NCEP\winds_speed\2006','E:\meta data\new\data _thesis\NCEP\winds_speed\2007','E:\meta data\new\data _thesis\NCEP\winds_speed\2008','E:\meta data\new\data _thesis\NCEP\winds_speed\2009','E:\meta data\new\data _thesis\NCEP\winds_speed\2010','E:\meta data\new\data _thesis\NCEP\winds_speed\2011','E:\meta data\new\data _thesis\NCEP\winds_speed\2012','E:\meta data\new\data _thesis\NCEP\winds_speed\2013','E:\meta data\new\data _thesis\NCEP\winds_speed\2014','E:\meta data\new\data _thesis\NCEP\winds_speed\2015'};
for ii=1:length(myfolder)
folder=char(myfolder{ii});
% B=num2str(cell2mat(myfolder));
sad_list = cmd_where(folder, 'sorted_lat_split_NCAR_NCEP.m' );
for sad = sad_list
ffs = fullfile( sad.dir, sad.name )
run( ffs )
end
end
Please check it, i sure it is fine to use?
Muhammad Usman Saleem
on 14 Apr 2016
3rd part of my question remains, which i have shared above,
I shall accept this answer after solving this query.? See above
per isakson
on 15 Apr 2016
"*3rd part* of my question remains?"   Yes, but here your problems come second.
per isakson
on 15 Apr 2016
"which i [sic] have shared above"   Be more specific! Do you mean the forth comment under your question. If so, does that description overtake all older descriptions?
Muhammad Usman Saleem
on 16 Apr 2016
I have done this task , thanks for your kind assistance. Please vote up my question, if you can
Usman
More Answers (1)
Image Analyst
on 12 Apr 2016
Use genpath() to get a list of all files in all subfolders. See attached demo.
4 Comments
Muhammad Usman Saleem
on 12 Apr 2016
Edited: per isakson
on 13 Apr 2016
I have tried this after updating @walter code (no idea whether i am correct or not?)
d = {'sorted_lat_split_NCAR_NCEP.m'};
basedir = 'E:\meta data\new\data _thesis\NCEP\winds_speed';
start_dir = pwd();
get_dirs = @(s){s(~ismember({s.name}, {'.', '..'}) & [s.isdir]).name};
yeardirs = get_dirs(dir(basedir));
yeardirs = cellfun(@(x)fullfile(basedir, x), yeardirs, 'uni', 0);
% Find year directories and turn into full paths
yeardirs = get_dirs(dir(basedir));
yeardirs = cellfun(@(x)fullfile(basedir, x), yeardirs, 'uni', 0);
for k = 1:numel(yeardirs)
% Find month directories and turn into full paths
monthdirs = get_dirs(dir(yeardirs{k}));
monthdirs = cellfun(@(x)fullfile(yeardirs{k}, x), monthdirs, 'uni', 0);
for m = 1:numel(monthdirs)
monthdirs = monthdirs{k};
for jj = 1 : numel(d)
toRun = fullfile(monthdirs, d{jj});
if exist(toRun, 'file')
try
run(toRun)
catch
warning([ 'Error encountered while processing file, ' ...
, 'continuing with other files: "%s"'], toRun );
end
end
end
end
end
cd(start_dir);
This code running .m file of only one folder not for other 12 folders
Here is output error appear
Export for pressure ID 0 -> p=1000.0hpa.
Export for pressure ID 1 -> p=925.0hpa.
Export for pressure ID 2 -> p=850.0hpa.
Export for pressure ID 3 -> p=700.0hpa.
Export for pressure ID 4 -> p=600.0hpa.
Export for pressure ID 5 -> p=500.0hpa.
Export for pressure ID 6 -> p=400.0hpa.
Export for pressure ID 7 -> p=300.0hpa.
Export for pressure ID 8 -> p=250.0hpa.
Export for pressure ID 9 -> p=200.0hpa.
Export for pressure ID 10 -> p=150.0hpa.
Export for pressure ID 11 -> p=100.0hpa.
Export for pressure ID 12 -> p=70.0hpa.
Export for pressure ID 13 -> p=50.0hpa.
Export for pressure ID 14 -> p=30.0hpa.
Export for pressure ID 15 -> p=20.0hpa.
Export for pressure ID 16 -> p=10.0hpa.
Cell contents reference from a non-cell array object.
Tell me how can i correct this error?
Image Analyst
on 12 Apr 2016
I don't know - it's not my code and you didn't give the complete error message so we don't know what line it's throwing the error on. Read this - it will help.
Muhammad Usman Saleem
on 13 Apr 2016
Edited: Muhammad Usman Saleem
on 13 Apr 2016
Keeping in view , about question and continuity of my sub folders.
The code, i shared above only running one mat file. Not running remaining folder .m files,
Export for pressure ID 0 -> p=1000.0hpa.
Export for pressure ID 1 -> p=925.0hpa.
Export for pressure ID 2 -> p=850.0hpa.
Export for pressure ID 3 -> p=700.0hpa.
Export for pressure ID 4 -> p=600.0hpa.
Export for pressure ID 5 -> p=500.0hpa.
Export for pressure ID 6 -> p=400.0hpa.
Export for pressure ID 7 -> p=300.0hpa.
Export for pressure ID 8 -> p=250.0hpa.
Export for pressure ID 9 -> p=200.0hpa.
Export for pressure ID 10 -> p=150.0hpa.
Export for pressure ID 11 -> p=100.0hpa.
Export for pressure ID 12 -> p=70.0hpa.
Export for pressure ID 13 -> p=50.0hpa.
Export for pressure ID 14 -> p=30.0hpa.
Export for pressure ID 15 -> p=20.0hpa.
Export for pressure ID 16 -> p=10.0hpa.
These lines are indication of running of sorted_lat_split_NCAR_NCEP.m in only one folder.
Then this error appear
Cell contents reference from a non-cell array object.
You can edit this code also can help me with your kind code?
Muhammad Usman Saleem
on 16 Apr 2016
I have done this task , thanks for your kind assistance. Please vote up my question, if you can
Usman
See Also
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)