Saving Data for all files to matrix
Mostrar comentarios más antiguos
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
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?
Stephen23
el 31 de Mayo de 2018
Debbie Oomen
el 31 de Mayo de 2018
Debbie Oomen
el 31 de Mayo de 2018
Editada: Debbie Oomen
el 31 de Mayo de 2018
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
el 31 de Mayo de 2018
"...but I cannot seem to get it to work"
Show us what you tried so far.
Paolo
el 31 de Mayo de 2018
What is AvgAwdiff initialised as?
Debbie Oomen
el 31 de Mayo de 2018
Debbie Oomen
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
el 31 de Mayo de 2018
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
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.
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
el 31 de Mayo de 2018
Editada: Guillaume
el 31 de Mayo de 2018
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?
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!