Info

This question is closed. Reopen it to edit or answer.

Can someone explain why this code doesn't have the desired output?

1 view (last 30 days)
I'm reading in an xls file
[num,txt,raw]=xlsread('fire9498500',1,'G1:BI11327','basic');
Chromium = cell(length(raw),2); %creating an empty array of cells
DateNumber=datenum(ActivityStartDate); %converting the dates into a format so MATLAB can interpret them
Date=num2cell(DateNumber); %making dates into a cell so they can be put into the cell arrays
for i=1:length(raw)
if strcmp(characteristic,'Chromium'), strcmp(unit,'Recoverable')
characteristic is a column from raw that has different heavy metals. I want to take the value of Chromium (in raw column 28) and put it in the empty Chromium array along with the respective date. Unit is another column from raw that tells if the value is Dissolved, Recoverable, Total, etc. I just want the Recoverable Chromium.
Chromium(i,1)=raw(i,28);
Chromium(i,2)=Date(i);
i=i+1;
end
end
I've also tried just doing one strcmp in the if statement but that didn't work either. Can someone tell me what I'm doing wrong please?

Answers (1)

Geoff Hayes
Geoff Hayes on 1 Mar 2015
Flannery - as MATLAB uses i and j to represent the imaginary number, please use a different indexing variable (say k) so that your for loop becomes
for k=1:length(raw)
Now look at your conditions for the if statement
if strcmp(characteristic,'Chromium'), strcmp(unit,'Recoverable')
First, how are characteristic and unit defined. You mention that characteristic is a column from raw but don't mention which column. If it is (for example) column 4, then you must initialize this variable as
characterisctic = raw{k,4};
You will also need to do something similar for unit. i.e.
unit = raw{k,?};
Then decide on how these two variables should be used as conditions in your if statement. Do both have to be true and so
if strcmpi(characteristic,'Chromium') && strcmpi(unit,'Recoverable')
or just one
if strcmpi(characteristic,'Chromium') || strcmpi(unit,'Recoverable')
In the above, we use && to indicate that the characteristic must be 'Chromium' AND the unit must be 'Recoverable'. In the second example, we use to indicate that the characteristic must be 'Chromium' OR the unit must be 'Recoverable'.
Note the use of strcmpi which ignores (upper or lower) case.
If the condition(s) are satisfied then the block of code to be executed is
Chromium(k,1)=raw(k,28);
Chromium(k,2)=Date(k);
k=k+1;
Note how k is used to access data from raw and Date and is also used to update Chromium. The third line then increments this value. This is incorrect as k is the indexing variable into raw and Date and so should not be incremented within this block of code - the incrementing will happen automatically on each iteration of the for loop. If you want a separate indexing variable for Chromium then you will need to create one.

Community Treasure Hunt

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

Start Hunting!