Issues with populating a matrix using for loop and if conditional statements
Show older comments
N= 200;
l_length = 1600;
fileToRead1='Length.xlsx';
sheetName='Sheet1';
[numbers, strings, raw] = xlsread(fileToRead1, sheetName);
if ~isempty(numbers)
newData1.vert_curves = numbers;
end
if ~isempty(strings)
newData1.textdata = strings;
end
if ~isempty(strings) && ~isempty(numbers)
[strRows, strCols] = size(strings);
[numRows, numCols] = size(numbers);
likelyRow = size(raw,1) - numRows;
% Break the data up into a new structure with one field per column.
if strCols == numCols && likelyRow > 0 && strRows >= likelyRow
newData1.colheaders = strings(likelyRow, :);
end
end
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
end
profile=numbers;
M1_return= 10;
M2_return= 20;
M3_return= 30;
for i= 2:N/2
for j=2:size(profile)
if profile(j,1)<round(0.25*l_length/2)
M1_carry_empty(i)=M1_return;
M2_carry_empty(i)= M2_return;
elseif profile(j,1)<round(0.75*l_length/2)
M3_carry_empty(i)=M3_return;
elseif profile(j,1)<round(0.9*l_length/2)
M1_carry_empty(i)=M1_return;
M2_carry_empty(i)= M2_return;
else
M1_carry_empty(i)=M1_return;
M2_carry_empty(i)= M2_return;
end
end
end
When i run the code, i expect that for M1_carry_empty will get populated with 10 from 2:24 & 91:100 . While M3_carry_empty would be populated with 20 from 25:75 and M3_carry_empty would be populated with 30 from 76:90. However, it doesn't do that and it only fills up at 2. I believe the problem might lie in this line.
for j=2:size(profile)
As the size of my profile is 161x5. The file length.xlsx is just a series of numbers from 0-1600 in increments of 10 and theyre all in the first column of the excel file.
The aim was,
for 200m<lengths>600m it would be populated with the M1_Carry_empty & M2_carry_empty.
for 200m>lengths<600m would be populated with M3_carry empty.
I have attached the excel file for reference. Please advise.
1 Comment
Bob Thompson
on 9 Jan 2019
The first thing you should do is specify a direction for size(). I assume you want to loop through all rows of 'profile' so the proper calling should be:
for j = 2:size(profile,1)
Unfortunately, I do not have the ability to view your excel file, so I can't test things directly, but if M1, M2, and M3 are only populationg cell 2 then you might need to look at your loop for 'i' rather than 'j'.
Answers (1)
Guillaume
on 9 Jan 2019
There are many issues with your code, the usage of the horror that is assignin, probably the biggest one. Nowadays, I would also recommend using readtable over xlsread.
" I believe the problem might lie in this line."
for j=2:size(profile)
It is certainly a poorly written line, but it actually shouldn't be a problem in your case. You should never pass an array to the right side of the colon (:) operator as I'm fairly certain that you don't know how colon deals with arrays. As Bob said, you should always specify the size dimension explictly, so:
for j = 2:size(profile, 1) %iterate over the rows
However, since colon uses the first element of the array, the two lines are equivalent.
There is certainly a problem with your i loop however. Other than the destination index, nothing inside the loop depends on i, so all elements of M1_carry_empty will have the same values, all elements of M2_carry_empty will have the same values, etc.
I suspect your j loop is also wrong. We have three possible destinations (M1_carry_empty(i), M2_carry_empty(i), and M3_carry_empty(i)) for however many elements there are in column 1 of profile, so the j loop is going to be overwriting these destinations a lot.
I'm actually not sure what it is you're trying to do. It's not clear what the it is in "it would be populated with ...", nor how the it can be populated by two values simultaneously ("it would be populated with the M1_Carry_empty & M2_carry_empty"). Most likely what you want to do involves a call to discretize and no loop at all.
6 Comments
Aleef Rahman
on 10 Jan 2019
Edited: Aleef Rahman
on 10 Jan 2019
Guillaume
on 10 Jan 2019
It looks to me that you don't understand loops. The i and j loop are not synchronised. The first iteration is indeed i=2 and j=1, however after that, you're still iterating inside the j loop, so i is still 2 and j is now 2. Since i is still 2, you're still filling the same element of Mx_xxx. Carry on until j = 161, then finally, you're moving to i=3 and j starts back at 1.
What you're trying to do is probably very simple but I still don't understand it. Why is it elements 2 to 25 and 76 to 100 that are populated? Where do the 2, 25, 76, and 100 come from?
Rather than giving us code that doesn't work, give us an example of input and the corresponding desired output.
Aleef Rahman
on 10 Jan 2019
Guillaume
on 10 Jan 2019
Screenshots are useless, it's not something we can manipulate in matlab and experiment with. Use mat files instead.
My question about 2, 25, 76 and 100 was why these values? I understand that they're the rows of Mx_xxx that you want filled, but why is it row 2 to 25 and not row 3 to 24 or any other value. Where do these numbers come from.
Aleef Rahman
on 15 Jan 2019
I'm sorry to say but you're really not clear. I'm actually not sure that it's even clear for you what it is you want to achieve.
So, for the first condition which was 2 to 25 What does a condition is 2 to 25 mean?, out of the 100 elements (N/2) 100 elements of what? i wanted 25% of what? to be filled with M1_carry_empty I was under the impression that M1_carry_empty was an array. Which elements of that array are used to fill the 25% of something?
So far, all I've understood is that we have an array
lengths = 10:10:1600;
and 3 arrays (Mxx_carry_empty) to fill with some values (where do the fill values come from) under some conditions. Perhaps if you wrote these conditions mathematically, it would be clearer. Please don't use matlab code since your code is obviously wrong.
"I have attached the mat file."
No, you've attached a m file of your code, which we already know doesn't work. I wanted a mat file of the expected result (the 3 Mxxx_carry_empty). Every time you mention the indices of these matrices you start at 2 (eg. 2:25). Why is 1 being ignored?
Perhaps, if you explained what all of this is going to be used for it would also be clearer.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!