how to write string value into perticular row by row incrementally on some condition

1 Ansicht (letzte 30 Tage)
tic; b=xlsread('F:\Research\K-Means Algorithm\Preprocessing.xls'); % Read xls file % [row,col]=size(b); % Row clol hold the number of crows and columns in the xls sheet j=1; for k=1:row % For k 1 to 230 do continur=e for loop
if(b(k,j)<2 && b(k,j)>=0.6)
b(k,k+1)='Normal'
end
if(b(k,j)>=2 && b(k,j)<5.5)
b(k,k+1)='Medium'
end
if(b(k,j)>5.5)
b(k,k+1)='High'
end
end

Antworten (1)

Matt Cohen
Matt Cohen am 18 Mai 2016
Bearbeitet: Matt Cohen am 18 Mai 2016
Hi Ravindra,
I understand that you are interested in replacing certain elements of arrays read in using "xlsread" with string values, given that these elements satisfy some condition.
I am not able to fully diagnose the issues without having the data or seeing the error messages you are receiving. However, one issue that you will likely encounter with your code is that you will not be able to add character arrays (strings) to the array 'b' because "xlsread" should return a numeric array. You cannot store characters in a numeric array. In order to add strings into 'b', you could convert it to a cell array using the "num2cell" function.
I have provided some example code below that shows a general workflow for a task like this. Instead of calling "xlsread" here, I have hard-coded an array that could be returned by that function. I am using "num2cell" to convert the numeric array to a cell array. Then I am iterating over each element in the array. In terms of the conditions, I am checking to see if each value is greater than 12 and less than 50; if so, I am replacing the value with the string 'Normal'. Note that I am using curly braces instead of parentheses to index into 'bCell'; this is how to index into a cell array in order to access the cell's contents.
b = [12, 97; 13, 98; 14, 99]; % Hard-coded numeric array
%b=xlsread(filename);
bCell = num2cell(b);
[row,col] = size(b);
for k=1:row
for j=1:col
if bCell{k,j}<50 && bCell{k,j}>12
bCell{k,j} = 'Normal';
end
end
end
I hope this information proves to be helpful.
Matt

Community Treasure Hunt

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

Start Hunting!

Translated by