code to load my files from folder, add workspace variable and save to another folder

Dears,
I have a lot of .mat files wich I want to add a variable fs=128 to all of the one by one.
one way is to load ech other, add variable and save, which takes some days because of thousands data.
so I need a code (i.e. for loop) to load each one, add same variable and save it.
please help me.
thank you.

 採用された回答

Stephen23
Stephen23 2020 年 10 月 22 日
編集済み: Stephen23 2020 年 10 月 22 日
You don't need to load the file data, you can use save's '-append' option:
fs = 128;
for ... loop over all files, you need to finish this line yourself!
fnm = 'name_of_the_file.mat'; % change this depending on how you generate/index the filenames.
save(fnm,'fs','-append')
end

3 件のコメント

Mohammad Zakaie Far
Mohammad Zakaie Far 2020 年 10 月 22 日
it is not work
Error using save
Unable to write file a: No such file or directory.
Error in untitled4 (line 4)
save(fnm,'fs','-append')
i don't know you understand my problem or not
i have a lot of file in one folder wich you can see in right of pic and want to take all of them fs=128
Stephen23
Stephen23 2020 年 10 月 22 日
編集済み: Stephen23 2020 年 10 月 22 日
"it is not work"
You will need to actually loop over the files using one of the methods shown in the documentation:
For example, something like this:
fs = 128;
D = 'absolute or relative path to the folder where the files are saved'; % !!! YOU NEED TO CHANGE THIS !!!
S = dir(fullfile(D,'*.mat'));
for 1:numel(S)
fnm = fullfile(D,S(k).name);
save(fnm,'fs','-append')
end
Mohammad Zakaie Far
Mohammad Zakaie Far 2020 年 10 月 22 日
thank you
you helped me a lot

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEnvironment and Settings についてさらに検索

製品

リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by