How to save data in cells to csv in loop
Show older comments
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
More Answers (1)
Sulaymon Eshkabilov
on 31 Dec 2022
0 votes
Why to use writetable()
2 Comments
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:
Categories
Find more on Standard File Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!