필터 지우기
필터 지우기

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
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.
Mikkel Eskildsen
Mikkel Eskildsen 2018년 11월 26일
Ah yes of course. Worked immediately. You have my vote if you want to make this an answer.
Thank you!

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

채택된 답변

Bob Thompson
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개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by