Writecell from iteration - append data to previous row

12 ビュー (過去 30 日間)
Mikel Jimenez
Mikel Jimenez 2023 年 2 月 25 日
コメント済み: Mikel Jimenez 2023 年 2 月 25 日
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 件のコメント
dpb
dpb 2023 年 2 月 25 日
編集済み: dpb 2023 年 2 月 25 日
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 2023 年 2 月 25 日
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.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by