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
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
2019-2-12
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
2019-2-13
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 .
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
2019-2-13
Walter Roberson
2019-2-13
table2struct(YourTable(3:end,:))
回答(0 个)
此问题已关闭。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!