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

 채택된 답변

Voss
Voss 2022년 12월 31일
편집: Voss 2022년 12월 31일

1 개 추천

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

Francisco
Francisco 2022년 12월 31일
편집: Francisco 2022년 12월 31일
Thank you, I'm going to try it out. From what I understand the empty cells are populated with nan. Would there be a way to keep them empty?
I'm getting the follwing error
Subscripted assignment dimension mismatch.
Voss
Voss 2022년 12월 31일
편집: Voss 2022년 12월 31일
Sorry, I had a mistake. Please try the corrected code.
Voss
Voss 2022년 12월 31일
편집: Voss 2022년 12월 31일
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 2022년 12월 31일
Thank you again. I'll give it a go!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022년 12월 31일

0 개 추천

Why to use writetable()

댓글 수: 2

Francisco
Francisco 2022년 12월 31일
편집: Francisco 2022년 12월 31일
Sorry I did not understand. From what I gather writetable needs columns of the same size.
Image Analyst
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에 대해 자세히 알아보기

태그

질문:

2022년 12월 31일

댓글:

2022년 12월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by