MATLAB Answers

Saving the selected .mat file and write it into csv

6 views (last 30 days)
steamrice
steamrice on 6 Feb 2020
Commented: Guillaume on 10 Feb 2020
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 Comments

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 6 Feb 2020
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 Comments

Walter Roberson
Walter Roberson on 9 Feb 2020
writetable(array2table(storedStructure.data, 'VariableNames', {'x', 'y', 'z'}), csvfilename));
The VariableNames set there has 3 elements. That command is only suitable for an array that has exactly 3 columns. For six columns, the cell array of character vectors would need 6 entries.
You can break the code up into several statements, like
T = array2table(storedStructure.data);
T.Properties.VariableNames = cell array with as many entries as data had columns
writetable(T, csvfilename)
steamrice
steamrice on 9 Feb 2020
@Walter Roberson I am still only work with the number of columns I have (according to my data). Other than that, its it possbile to save out the user input (from prompt) and write it into the same .csv?

Sign in to comment.

More Answers (1)

steamrice
steamrice on 10 Feb 2020
Thanks for the response!
I think you mentioned, "I need to emphasize that this cannot be used to add new columns to a csv file."
Does that mean its not possbile to combine the user input from prompt with the after-data-anaylsis into the same .csv?

  4 Comments

Show 1 older comment
Walter Roberson
Walter Roberson on 10 Feb 2020
Does that mean its not possbile to combine the user input from prompt with the after-data-anaylsis into the same .csv?
No, it does not mean that.
If you have an existing .csv file and you want to add new rows to it, then you can do that by appending to the file without changing what is already there.
If you have an existing .csv file and you want to add new columns to it, then there is no way to do that without changing what is already there. None of the Mathworks-supplied functions can add new columns to a .csv file: they can only add new rows or they can rewrite the entire file, destroying whatever is currently in it.
In other words, if you want to add new columns to a .csv, then what you need to do is read in all of the existing data from the .csv, and add more columns to what you read in, and then write the old data together with the new data overtop of the file.
It is simply not technically possible to add new columns to a text file without rewriting from the end of the old first line completely through to the end of the file -- but by the same measure, if you do bother to read in the existing data, you can certainly write it out with new data as well.
steamrice
steamrice on 10 Feb 2020
Question: If adding new row, wouldn't it affect my data (three column) as well?
In addition, would you mind direct me if I want to try this method?
"if you want to add new columns to a .csv, then what you need to do is read in all of the existing data from the .csv, and add more columns to what you read in, and then write the old data together with the new data overtop of the file."
Guillaume
Guillaume on 10 Feb 2020
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.

Sign in to comment.

Sign in to answer this question.

Tags

Products