How to save data in cells to csv in loop
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
Sulaymon Eshkabilov
2022년 12월 31일
0 개 추천
Why to use writetable()
댓글 수: 2
Image Analyst
2022년 12월 31일
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
카테고리
도움말 센터 및 File Exchange에서 Standard File Formats에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!