Writecell from iteration - append data to previous row

14 views (last 30 days)
Hi,
I need to write data from a loop to a csv file. However, each row should combine data from trial i with data from trial i-1. Would that be possible somehow? Here my code simplified:
headers = ["Some_columns_from_trial_i","Some_columns_from_trial_i-1"]
writematrix(headers,"FileName",'Delimiter',';'); %csv file
%% Start trial loop
for trial = 1:n_trial
%% Collect response (for trial-1)
if trial==1
break;
else % Check response
%Here I get the data for "Some_colums_from_trial_i-1"
%% Save results
resultsArray=rot90(struct2cell(results));
writecell(resultsArray, "FileName",'Delimiter',';','FileType','text','WriteMode','append'); %This should be saved on row i-1
end
break
%% Here I get the data for "Some_columns_from_trial_i"
resultsArray=rot90(struct2cell(results));
writecell(resultsArray, "FileName",'Delimiter',';','FileType','text','WriteMode','append'); %This should be saved on row i
end
Would anyone know if there is any option to do this, maybe telling writecell to append to row i-1? I cannot find the trick so far.
Thanks in advance.
  2 Comments
dpb
dpb on 25 Feb 2023
Edited: dpb on 25 Feb 2023
The code is very difficult to parse to figure out what is actually going on without any data to illustrate -- a fully-working mini-sample would help immensely and up the odds of somebody actually trying to solve the problem orders of magnitude, I'd wager.
Taking a guess at what you're trying to do, I suspect the answer is to not try to write every loop but only ever other pass and build the record in memory first, then write it.
'append' does not work in combination with an address range (and a .csv file doesn't know about ranges, anyway).
The alternative could be to use low-level i/o and write each segment as you retrieve it, but only add the newline output every other write (or after the second column only). The higher level routines add the newline automagically for each element/row each time they're called; low-level i/o with fprintf won't add the newline until you specifically write it to the file.
The other reason to use the latter approach if you can't build the whole output array in memory first, then write it in one call to writecell is that it opens/closes the file every time it is called; this is quite inefficient in a tight loop. It probably won't cause an issue with the .csv file but I have found that if tried to update spreadsheet files on a cell-by-cell or small range basis in such a similar tight loop that the writeXXXX family will eventually crash and burn if the loop is tight/long enough; apparently Excel simply can't keep up. I've not tried to see about the text files; I do tend to try to ensure don't use these routines in that fashion.
Mikel Jimenez
Mikel Jimenez on 25 Feb 2023
I think I have figure it out using writecell and saving to an excel file. This way you can specify the row and column you want to write your data to. Thanks.

Sign in to comment.

Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!