Saving Data for all files to matrix

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 comentarios

Guillaume
Guillaume el 30 de Mayo de 2018
  • I don't see an actual question and it's really not clear what it is you want to achieve
  • Nothing in your loop depends on k. What is the purpose of the loop?
  • Time and time again we say on this forum that numbered variables are a very bad design. Why do you have 10 different variables DxxAwdiff instead of a single matrix or cell array?
Debbie Oomen
Debbie Oomen el 31 de Mayo de 2018
I need the output of AvgAwdiff for the length of FNM
Debbie Oomen
Debbie Oomen el 31 de Mayo de 2018
Editada: Debbie Oomen el 31 de Mayo de 2018
I keep on finding these easy solutions where you just need to put your loop variable (in my case, k) behind the variable you want to save the iteration from but I cannot seem to get it to work. So my question is, how can I save AvgAwdiff for the length of FNM to a matrix or array
Ankita Bansal
Ankita Bansal el 31 de Mayo de 2018
Hi Debbie, can you post the question for which you are trying to find an answer?
Stephen23
Stephen23 el 31 de Mayo de 2018
"...but I cannot seem to get it to work"
Show us what you tried so far.
Paolo
Paolo el 31 de Mayo de 2018
What is AvgAwdiff initialised as?
for k = 1:length(FNM)
AvgAwdiff = abs(D1Awdiff + D2Awdiff + D3Awdiff + D4Awdiff + D5Awdiff + D6Awdiff + D7Awdiff + D8Awdiff +...
D9Awdiff + D10Awdiff) / length(TallyCheck);
AvgAwdiff= round(AvgAwdiff);
AvgAwdiff(k,:) = num2str(AvgAwdiff)
end
for k = 1:length(FNM)
AvgAwdiff = abs(D1Awdiff + D2Awdiff + D3Awdiff + D4Awdiff + D5Awdiff + D6Awdiff + D7Awdiff + D8Awdiff +...
D9Awdiff + D10Awdiff) / length(TallyCheck);
AvgAwdiff= round(AvgAwdiff);
AvgAwdiff(k) = num2str(AvgAwdiff)
end
I have also tried using (k) behind the equal sign but that does not work either. It just gives me the error that the sides are not equal. To be clear, the k loop starts way earlier but this is just so everyone can see that it is in a loop. The AvgAwdiff is just a number but I want the number for the length of FNM in a matrix
Stephen23
Stephen23 el 31 de Mayo de 2018
Editada: Stephen23 el 31 de Mayo de 2018
@Debbie Oomen: if AvgAwdiff is a number why are you trying to force a string into it? What do you want that code to do?
Debbie Oomen
Debbie Oomen el 31 de Mayo de 2018
The num2str is because it is also displayed in a text afterwards. But that can be ignored. The main thing is that the values are saved in one matrix
Paolo
Paolo el 31 de Mayo de 2018
Editada: Paolo el 31 de Mayo de 2018
Perhaps you should initialize AvgAwdiff.
AvgAwdiff = zeros(k,1).
if you just need to store a single value for each iteration.
Debbie Oomen
Debbie Oomen el 31 de Mayo de 2018
Then this gives me the following error:
Index in position 1 exceeds array bounds (must not exceed 1).
Stephen23
Stephen23 el 31 de Mayo de 2018
@Debbie Oomen: why not just use a preallocated cell array, like I showed you in my answer to your earlier question?
Debbie Oomen
Debbie Oomen el 31 de Mayo de 2018
You mean using {k} instead? I am sorry but I just can't seem to understand this..
Stephen23
Stephen23 el 31 de Mayo de 2018
Editada: Stephen23 el 31 de Mayo de 2018
Thank is fine, there are lots of things to learn. Take deep breath, have a coffee, relax a little.
One of the most basic (and important!) concepts in MATLAB (and many other programming languages) is indexing. The introductory tutorials explain how indexing works:
Cell array indexing is really simple:
  • () parentheses refer to the cells themselves.
  • {} curly braces refer to the contents of the cells.
For a more detailed explanation, see:
What indexing to use, or even whether to use cell array or a numeric array, depends on your algorithm and data, which I don't know anything about. You could use a numeric array to import file data if it has the same size in each file. If the data size or class changes between files then a cell array might be easier. In my other answer, I used a cell array just to make the code simpler.
Guillaume
Guillaume el 31 de Mayo de 2018
Editada: Guillaume el 31 de Mayo de 2018
At the moment, it's very unclear what the inputs are and what the desired output is. Can you give a short example of the inputs, using valid syntax and of the output with a much better description of the desired output. "the output of AvgAwdiff for the length of FNM" doesn't mean anything to us.
Input example:
FNM = [1 2 3 4 5];
D1AWdiff = [10 20 30 40 50];
D2AWdiff = ....
...
TallyCheck = [-1 2 10];
Desired output:
AvgAwdiff = ????
because ...
Use as many words as necessary to explain of AvgAwdiff is derived from the input variables
Debbie Oomen
Debbie Oomen el 31 de Mayo de 2018
Editada: Guillaume el 31 de Mayo de 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 el 31 de Mayo de 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?

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 31 de Mayo de 2018
Editada: Stephen23 el 31 de Mayo de 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)

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 30 de Mayo de 2018

Comentada:

el 31 de Mayo de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by