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 个评论
Guillaume
2018-5-30
- 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
2018-5-31
Debbie Oomen
2018-5-31
Debbie Oomen
2018-5-31
编辑:Debbie Oomen
2018-5-31
Ankita Bansal
2018-5-31
Hi Debbie, can you post the question for which you are trying to find an answer?
Stephen23
2018-5-31
"...but I cannot seem to get it to work"
Show us what you tried so far.
Paolo
2018-5-31
What is AvgAwdiff initialised as?
Debbie Oomen
2018-5-31
Debbie Oomen
2018-5-31
Perhaps you should initialize AvgAwdiff.
AvgAwdiff = zeros(k,1).
if you just need to store a single value for each iteration.
Debbie Oomen
2018-5-31
Stephen23
2018-5-31
@Debbie Oomen: why not just use a preallocated cell array, like I showed you in my answer to your earlier question?
Debbie Oomen
2018-5-31
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
2018-5-31
编辑:Guillaume
2018-5-31
Guillaume
2018-5-31
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?
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!