Saving Data for all files to matrix

5 vues (au cours des 30 derniers jours)
Debbie Oomen
Debbie Oomen le 30 Mai 2018
Commenté : Guillaume le 31 Mai 2018
I have seen this question loads of times and have also tried different answers but I cannot seem to find the correct one. The following code is in a loop:
for k = 1:length(FNM)
AvgAwdiff = abs(D1Awdiff + D2Awdiff + D3Awdiff + D4Awdiff + D5Awdiff + D6Awdiff + ...
D7Awdiff + D8Awdiff + D9Awdiff + D10Awdiff) / length(TallyCheck);
AvgAwdiff= round(AvgAwdiff);
AvgAwdiff = num2str(AvgAwdiff)
end
I have tried AvgAwdiff(k,:) and AvgAwdiff(k) or AvgAwdiff{k}. But these either give me an error or a character that does not have the correct values. I followed the data in the command window and everything works fine but it is just the save the values in a matrix that I can't seem to do.
  18 commentaires
Debbie Oomen
Debbie Oomen le 31 Mai 2018
Modifié(e) : Guillaume le 31 Mai 2018
FNM is a (in this case) 50x1 cell array. These are the first couple:
'ID117.csv'
'ID118.csv'
'ID123.csv'
'ID128.csv'
'ID132.csv'
'ID134.csv'
'ID135.csv'
AWdiff stands for awake difference per day and are always a number:
D1AWdiff= -7.4
D2AWdiff = 4.1
D3AWdiff = 1.2, etc...
TallyCheck = [1234 3242 4352 5464 6574 2353].
These are the datapoints per day. There is never more data than 10 days for my script but the amount of days can vary per FNM. So maybe ID117.csv has 5 days of data and ID118 has 7 days of data. The length of the TallyCheck gives me the number of days.
So what I need is to get the average of the awake differences over the amount of days but then for all of the FNM files. SO therefor I sum up all of the awake differences per day and divide it by the amount days in the TallyCheck.
So ideally I would like this output:
FNM AvgAwdiff
ID117.csv 48
ID118.csv 23
ID123.csv 89
. .
. .
and so on for the length of FNM
I hope this explains it a bit better
Guillaume
Guillaume le 31 Mai 2018
Yes, this is a lot clearer. A few questions:
  • Do the DxAWdiff and TallyCheck vary per FNM (something you haven't shown so far) or are they constant throughout?
  • Is your problem that you want to sum a different number of DxAWdiff variable per FNM (and you don't know how to do that?
  • How are the DxAWdiff created?

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 31 Mai 2018
Modifié(e) : Stephen23 le 31 Mai 2018
You need to reference the output variable using indexing, otherwise you simply overwrite it on each loop iteration. Assuming that on each iteration you calculate exactly one value, then try something like this:
AvgAwdiff = nan(size(FNM)); % preallocate!
for k = 1:numel(FNM)
... whatever calculations that depend on k.
tmp = D1Awdiff + D2Awdiff + D3Awdiff + D4Awdiff + D5Awdiff + D6Awdiff + D7Awdiff + D8Awdiff + D9Awdiff + D10Awdiff;
tmp = abs(tmp) / length(TallyCheck);
AvgAwdiff(k) = round(tmp);
end
See also:
PS: Numbered variables are generally a sign that you should be using arrays and indexing. For example, rather than D1Awdiff, D2Awdiff, etc. putting the data into one matrix D1wdiff would mean you could simply do this:
sum(D1wdiff)

Plus de réponses (0)

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by