Saving the selected .mat file and write it into csv
4 ビュー (過去 30 日間)
古いコメントを表示
Hello there,
I was implmenting something similar from this post: https://www.mathworks.com/matlabcentral/answers/317297-uigetfile-load-m-file-variables-into-workspace and I want to get the selected .mat file and write it into csv. I have the following but I have the problem of saving it into .csv file
[baseFileName, folder] = uigetfile('*.mat');
fullFileName = fullfile(folder, baseFileName);
if exist(fullFileName, 'file')
storedStructure = load(fullFileName);
else
warningMessage = sprintf('Warning: mat file does not exist:\n%s', fullFileName);
uiwait(errordlg(warningMessage));
return;
end
cHeader = {'x' 'y' 'z'}; %dummy header
commaHeader = [cHeader;repmat({','},1,numel(cHeader))]; %insert commaas
commaHeader = commaHeader(:)';
textHeader = cell2mat(commaHeader); %cHeader in text with commas
%write header to file
%write data and save it to .csv
fid = fopen(fullFileName,'w');
fprintf(fid,'%s \n',textHeader);
fclose(fid);
dlmwrite(fullFileName,storedStructure.data,'-append');
I think I have problem in the secotion of %write data and save it to .csv using fullFileName. I do not know how to do it properly since we do not have one fixed file to write into. Thanks!
0 件のコメント
採用された回答
Guillaume
2020 年 2 月 6 日
Assuming that storedStructure.data is a matrix with 3 columns, the simplest way to save to csv is with:
writetable(array2table(storedStructure.data, 'VariableNames', {'x', 'y', 'z'}), fullfilename, 'FileType', 'text');
Note that I would recommend changing the extension of the filename from .mat to .csv so that it's not misleading.
18 件のコメント
Walter Roberson
2020 年 2 月 9 日
As this is for csv files, see my discussion at https://www.mathworks.com/matlabcentral/answers/503384-xlswrite-overwrites-data-despite-setting-a-specific-range#answer_413819
その他の回答 (1 件)
steamrice
2020 年 2 月 10 日
4 件のコメント
Guillaume
2020 年 2 月 10 日
As said, you'd be better off starting a new question. You'd likely get more contributors.
"If adding new row, wouldn't it affect my data"
Not sure what you mean by that. new rows are added to the end of the file by using the OS file append mode. This does not touch data earlier in the file.
"would you mind direct me if I want to try [adding new columns]"
Here you need to a number of difficulties. Do you need to preserve the exact text formatting of the earlier columns, including number format, separator(s) (type and number if using spaces/tabs for example), etc.? Do you have to detect the formatting or can your code just assume the formatting?
The simplest way, which may not preserve the formatting is to read your file with your favorite csv reading function (readmatrix, readtable, csvread, dlmread, importdata (avoid that one)), append your new columns to the data and rewrite with the matching function to the one you used to read (writematrix, writetable, csvwrite, dlmwrite).
olddata = readmatrix(somefile);
appended = [olddata, newdata]; %newdata must be a matrix with as many rows as olddata
writematrix(appended, somenewfile);
If you need to preserve formatting and autodetect the formatting, it could be significantly more complicated.
参考
カテゴリ
Help Center および File Exchange で Startup and Shutdown についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!