Help with looping through columns from .csv-file and writing data to .xlsx-file
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Mikkel Eskildsen
il 23 Nov 2018
Risposto: Bob Thompson
il 26 Nov 2018
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 Commenti
Bob Thompson
il 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.
Risposta accettata
Bob Thompson
il 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.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!