Saving array values as .mat file

Hello All,
What I am trying to do is I have bunch of .csv files. Most of them have autogenerated garbage things written before the actual tables are available.
So far I was able to remove this garbage thing and could able to save only tables with following code.
Next thing I want is to save that table from each csv file as .mat which I am not able to do.
This code is generating .mat file but have "x" array in it. Instead I want to have all the data inside x in that mat file.
clear all
close all
d = uigetdir();
filePattern = fullfile(d, '*.csv');
file = dir(filePattern);
x = cell(1, numel(file));
for k = 1 : numel(file)
baseFileName = file(k).name;
fullFileName = fullfile(d, baseFileName);
x{k} = readtable(fullFileName);
fprintf('read file %s\n', fullFileName);
fprintf('read file %s\n', baseFileName);
writetable(x{k},fullFileName);
%MyFile=strcat(baseFileName);
save([baseFileName '.mat'],'x')
end
I hope this is making any sense.

11 件のコメント

Adam
Adam 2019 年 2 月 12 日
What do you mean by "x" array? Why are you saving the mat file in a loop though? Surely you want to save it at the end of the loop since it is x you are saving and x is being written to iniside the loop?
adi kul
adi kul 2019 年 2 月 12 日
so basically "x" here stores all the "tables" from the all the .csv files I have in the directory.
Now suppose ABC.csv have a 233x2122 table with some garbish things mentioned of 1st 20 rows. I want to save it as ABC.mat with same 233x2122 tables.
So far I could remove the garbish and was able to save ABC.csv with just 233x2122 table but with above code I am getting ABC.mat with "x" array in it with 233x2122 size.
Adam
Adam 2019 年 2 月 12 日
But saving in a loop as you are you still only get the final result anyway, everything prior to that was overwritten, and that final result should be whatever x is at the end of the final loop, which surely should be a cell array whose length is the number of files. If you want to save one result per file why are you adding all your files into your single x variable?
adi kul
adi kul 2019 年 2 月 12 日
編集済み: adi kul 2019 年 2 月 12 日
I used loop beacause I want to save that perticular file with .mat
So each time the loop executes for say 5 .csv files, it will create 5 .mat files with only tables in it. Not a variable with table in it!
now this code is creating ABC.mat but when I open it, it will have x array of size 233x2122 instead I want to save ABC.mat with array size 233x2122. so no "x" in it!
adi kul
adi kul 2019 年 2 月 13 日
No answers till now.
Please let me know if is it even possible?
Walter Roberson
Walter Roberson 2019 年 2 月 13 日
No. .mat files always have to have variables stored in them. You cannot just store an array in ABC.mat without an array name .
adi kul
adi kul 2019 年 2 月 13 日
編集済み: adi kul 2019 年 2 月 13 日
Any way I can save table headers as array names to save in .mat?
so suppose it's having 200 columns with 2400 rows, I will create 200 arrays with each will have corresponding rows as data like 2400x1 ?
Walter Roberson
Walter Roberson 2019 年 2 月 13 日
yes. table2struct with ToScalar option. Then save() the struct with the -struct option. Each field will become a separate variable .
adi kul
adi kul 2019 年 2 月 13 日
Great. I could do it. Now one more cleaning I want to do is removing 1st 2 rows from all the .mat files it created. any suggestion on it?
Walter Roberson
Walter Roberson 2019 年 2 月 13 日
table2struct(YourTable(3:end,:))
adi kul
adi kul 2019 年 2 月 13 日
編集済み: adi kul 2019 年 2 月 13 日
can I get it to double array instead of cell array ?
Edit:
I will close this question and open a new as the question I asked here is solved. I will open new question for cell array to matrix conversion.

回答 (0 件)

この質問は閉じられています。

製品

リリース

R2017a

タグ

質問済み:

2019 年 2 月 12 日

閉鎖済み:

2019 年 2 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by