how to automatically open files and do the commands

7 ビュー (過去 30 日間)
yc j
yc j 2016 年 1 月 26 日
コメント済み: Image Analyst 2016 年 1 月 27 日
You know how in a simple macro software, I give command such as
"click window explore(directory where the data are saved) -> arrow down(to select the next data file) -> enter(open file) -> do what u gotta do -> save & exit"
so that the macro opens the files below the one before over and over....
How do i set the command such as above in matlab?
I have files that has inconsistent file names, such as 1.txt, 1.2.txt, 2.txt, 10.txt, 21.txt....

採用された回答

jgg
jgg 2016 年 1 月 26 日
You'll have to program this, but it's pretty straightforward.
cd 'C:\Users\etc' %your file path with all the files.
c = struct2cell(dir('*.txt')); %get a listing of all the files with extension .txt in the directory
for i = 1:size(l,1)
fileID = fopen(c{1,i});
doWhatUGottaDo(fileID);
end
The cell{1,i} contains the name of the file, which you can then use to open it and do stuff to it. How you proceed from here depends on what you want to do.
  5 件のコメント
yc j
yc j 2016 年 1 月 27 日
I somehow managed to avoid using 1:size(c,2)
The problem I was having was that other way gave me '0.5', not 0.5.. which is matrix or stray i guess... when i subracted 0, it gave me something like [48 46 53]... in which 48 meant 0, 46 meant . and 53 meant 5 in '0.5'. weird... I just simply could have said b=c{1,i}... but since i put this much time on it, I just used simple math to give relation between the numbers and the [48 46 53] matrices...
Thank you again for the answer! the first two line was the key by the way!
Image Analyst
Image Analyst 2016 年 1 月 27 日
Or you could have used the simpler way in the FAQ, which avoids cell arrays altogether and just uses the simple structure array returned by dir(). I see no need to stuff it into a cell array when you can get the filename simply and directly from the structure. It just makes it more complicated for no reason/benefit.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 1 月 26 日
  1 件のコメント
yc j
yc j 2016 年 1 月 26 日
thank you very much for the answer!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by