Effacer les filtres
Effacer les filtres

Help with looping through columns from .csv-file and writing data to .xlsx-file

3 vues (au cours des 30 derniers jours)
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 commentaires
Bob Thompson
Bob Thompson le 23 Nov 2018
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 le 26 Nov 2018
Ah yes of course. Worked immediately. You have my vote if you want to make this an answer.
Thank you!

Connectez-vous pour commenter.

Réponse acceptée

Bob Thompson
Bob Thompson le 26 Nov 2018
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.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by