Help with looping through columns from .csv-file and writing data to .xlsx-file
3 ビュー (過去 30 日間)
古いコメントを表示
I have written a script that is supposed to do some calculations on a .csv-file that the user has chosen, and write the results to different columns in an .xlsx-file.
I have a loop that runs through different letters for a variable that decides which column in the .xlsx should have the written data - this works. The part that doesn't is the loop with an array, rfd200left(k), that calculates a number on 5 different columns in the .csv-file. What I get in the columns of the .xlsx is the same number on all 5 cells (I get the last calculated number of the loop. I tried moving around almost everything, but I can't find the flaw. The code is below, and the .csv-file is attached for context. Let me know if any additional information is needed.
This first part is a browse-button, that let's the user browse for a csv-file to be read.
function vaelgPushed(app, event)
%Browse files
[filename, pathname] = uigetfile('*.csv');
app.UIFigure.Visible = 'off';
app.UIFigure.Visible = 'on';
app.fullpathname = strcat(pathname, filename);
app.valgt.Value = app.fullpathname;
% Read csv-file
app.rfdtest = csvread(app.fullpathname, 8, 1);
end
The next part is the button that analyzes the csv-file, and writes calculated data to an excel-document. This is where my loop is doing something wrong at the "writetable"-part:
function analyserPushed(app, event)
rfdtest = app.rfdtest;
fullpathname = app.fullpathname;
threshold = 1.5;
newton = 4.44822162;
for k = 1 : 5
left = rfdtest(:, k);
if left(1)>0
left=[0;left] % If the first item in a column is not zero, add zero.
end
for letter='E':'I'
column = [letter,'2']
% Variables for calculations
indexoverLeft = find(left>threshold,1);
P1Left = left(indexoverLeft,1);
prethreshLeft = indexoverLeft-1;
P2Left = left(prethreshLeft+21);
% -- Find RFD 200 ms [Pounds/sek]
if (P2Left-P1Left)/0.2 > 0
rfd200left(k) = (P2Left-P1Left)/0.2;
T = table(rfd200left(k));
writetable(T,'rfd_skitse_kopi.xlsx', 'range', column, 'WriteVariableNames', false)
end
end
end
end
4 件のコメント
Bob Thompson
2018 年 11 月 23 日
It is the presence of the internal letter loop that is causing your problem. You are writing all five cells for each time you run the k loop. I think you could solve your problem by removing the internal loop, turning T into a matrix, and printing all of T at one time after the k loop.
採用された回答
Bob Thompson
2018 年 11 月 26 日
It is the presence of the internal letter loop that is causing your problem. You are writing all five cells for each time you run the k loop. I think you could solve your problem by removing the internal loop, turning T into a matrix, and printing all of T at one time after the k loop.
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!