chris
chris

Reputation: 300

Convert all *.mat files in a directory to *.txt files?

I want to convert all *.mat files in a directory to corresponding *.txt files example, input : 100.mat output: 100.txt

I use the following code,

files = dir(‘*.mat’);
for file = files'
    mat = load(file.name);
    % Do some stuff
    %Conten = who;
    save('file.txt', Conten{:}, '-ascii')
end

but it returns all the lines into one big text file or just the content of the last file. I want to get all files converted to *.txt with the corresponding filenames.

Upvotes: 1

Views: 869

Answers (2)

Stalin Samuel
Stalin Samuel

Reputation: 45

files = dir('*.mat');
for file = 1:files
    mat = load(files(file).name);
    % Do some stuff
    %Conten = who;
    F_current=files(file).name
    F_name = strcat(a1(1:end-3),'txt')
    save(F_name, Conten{:}, '-ascii')
end

Upvotes: 1

Y.Chen
Y.Chen

Reputation: 11

Some basic mistakes..... Watch out save function, as you specified as:

save('file.txt', Conten{:}, '-ascii')

it indicates save everything into file.txt. Of cause you get that result. In order to save into different file, you needs to generate file name every time.

something like these:
files = dir('*.mat');
for file = files'
    mat = load(file.name);
    % Do some stuff;
    %Conten = who;
    save(strcat(file.name, '.txt'), Conten{:}, '-ascii');
end

Upvotes: 1

Related Questions