How to save data in cells to csv in loop

18 views (last 30 days)
I have a struct with different fields from which I extract 120 columns of data in a loop. I prepend a number to each column and I'm now trying to save these columns to a csv file. All column have different lengths and I need the csv to be have these columns side-by-side. Also, I'm trying to use the row offset to prepend 6 empty rows to each column while writing to the csv. I tried to play with the parameters but csvwrite doesn't write the columns side-by-side. I'm not sure how to proceed from here.
n_chans = size(file.dat, 1); % get number of channels. Should be 120
for chan = 1:n_chans
% get all timestamps from each channel
timestamps = file.dat{chan, 1}.timestamps * 1000000; % convert seconds to microseconds
timestamps = num2cell(timestamps);
% get channel info
full_name = file.dat{chan, 1}.name;
underscore_indices = strfind(full_name, '_');
channel_number = str2double(full_name(underscore_indices(3)+1 : underscore_indices(4)-1));
% channel data as column
column = [channel_number; timestamps]; % add the channel name/number
% save data
csvwrite('test.csv', column, 6, col_offset); % write with 6 row offset
col_offset = col_offset + 1;
end

Accepted Answer

Voss
Voss on 31 Dec 2022
Edited: Voss on 31 Dec 2022
n_chans = size(file.dat, 1); % get number of channels. Should be 120
data = NaN(0,n_chans); % initialize matrix to be written to file
n_rows = 0; % number of rows in data, initially 0, will be increased below as necessary
column_offset = 1;
for chan = 1:n_chans
% get all timestamps from each channel
timestamps = file.dat{chan, 1}.timestamps * 1000000; % convert seconds to microseconds
% get channel info
full_name = file.dat{chan, 1}.name;
underscore_indices = strfind(full_name, '_');
channel_number = str2double(full_name(underscore_indices(3)+1 : underscore_indices(4)-1));
% channel data as column
column = [channel_number; timestamps]; % add the channel name/number
% increase the number of rows of data as necessary
nr = numel(column);
if n_rows < nr
data(n_rows+1:nr,:) = NaN;
n_rows = nr;
end
% store column in data
data(1:nr,col_offset) = column;
col_offset = col_offset + 1;
end
% prepend 6 blank rows to data
data = [NaN(6,n_chans); data];
% write data to file
csvwrite('test.csv',data);
% or use writematrix
% writematrix(data,'test.csv');
  5 Comments
Voss
Voss on 31 Dec 2022
Edited: Voss on 31 Dec 2022
One way to have blank cells is to convert the matrix to a cell array, replace NaN element with [] (empty numeric array) or '' (empty character vector), and use writecell to write it
nandata = isnan(data);
data = num2cell(data);
data(nandata) = {[]};
writecell(data,'test.csv');
Francisco
Francisco on 31 Dec 2022
Thank you again. I'll give it a go!

Sign in to comment.

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 31 Dec 2022
Why to use writetable()
  2 Comments
Francisco
Francisco on 31 Dec 2022
Edited: Francisco on 31 Dec 2022
Sorry I did not understand. From what I gather writetable needs columns of the same size.
Image Analyst
Image Analyst on 31 Dec 2022
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!