Convert all csv in a folder to .mat
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I have several csv files in one folder and I want to convert them all to .mat format?
Is it possible to do that at once?
採用された回答
Folder = 'D:\YourFolder';
FileList = dir(fullfile(Folder, '*.csv'));
for iFile = 1:numel(FileList)
% [TYPO FIXED]: [name, ext] ==> [~, name, ext]
[~, name, ext] = fileparts(FileList(iFile).name);
data = readtable(fullfile(Folder, [name, ext]));
save(fullfile(Folder, [name, '.mat']), 'data');
end
10 件のコメント
Elise Barbeau
2021 年 2 月 3 日
Thank you! Also, once I have my .mat files, if I want for all the files to remove the first line , how can I apply this to all the files in my folder? myfile(1,:) = []
Elise Barbeau
2021 年 2 月 3 日
I get an error message for your conversion script above, it's looking for txt files
Error using readtable (line 223)
Unable to open file
'C:\mypath\aFileInTheFolder.txt'
there are not txt files in my Folder only .csv (coma delimited)
Jan
2021 年 2 月 4 日
My code processes .csv files only:
FileList = dir(fullfile(Folder, '*.csv'));
If your code does somoething else, please post your code, such that we can search for the problem.
"once I have my .mat files, if I want for all the files to remove the first line" - the first line of what? Maye you mean the first line of data ? readtable() was just a suggestion. Use a method, which matchs your needs.
Elise Barbeau
2021 年 2 月 4 日
I know, I only have .csv files in my folder, that's why I don't understand that I get an error message unable to open file and it wants to open a .txt file (giving my the name of my first file in my folder but with .txt at the end rather than .csv), is there a default that I need to change in the code you gave me?
Jan
2021 年 2 月 4 日
This was y typo in my code. Please try the fixed version above.
Elise Barbeau
2021 年 2 月 5 日
Yes! thank you
Elise Barbeau
2021 年 2 月 5 日
now my other question ;-) it's kind of unrelated but now I would to delete the first row of data for all those .mat data files....
Jan
2021 年 2 月 5 日
What about inserting this in the code above:
data(1, :) = [];
Elise Barbeau
2021 年 2 月 5 日
yes I saw that too but to do it on all the files in my folder at once?
Jan
2021 年 2 月 5 日
This suggestion does it during the creation already. Then you do not have to do it again. But the code looks similar to the one of my answer:
Folder = 'D:\YourFolder';
FileList = dir(fullfile(Folder, '*.mat'));
for iFile = 1:numel(FileList)
FileData = load(fullfile(Folder, FileList(iFile).name));
data = FileData.data;
data(1, :) = [];
save(fullfile(Folder, [name, '.mat']), 'data');
end
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Standard File Formats についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
