whats wrong with this code?
Afficher commentaires plus anciens
Hi I am getting the disp output four times instead of three which is correct and should be displayed after executing this code:
function where_is_solution_required(lit_kolonne)
form_uten_tiltak='SANDSTEIN';
for k=1:size(lit_kolonne,1)
my_lit=lit_kolonne(k);
if (strcmpi(my_lit,form_uten_tiltak))
continue
end
disp('Tiltaket for sonen settes i gang')
end
end
3 commentaires
Adam
le 11 Jan 2019
How could we possibly know without knowing what the input is? Also please format your code using the 'CODE' section so it is more readable with indentations and without so many blank lines.
Muazma Ali
le 11 Jan 2019
Guillaume
le 11 Jan 2019
Your reasoning is correct. Therefore we can surmise that your input does not contain 4 cells, or that it contains 4 cells and that none of them contains sandstein. The only way to know for sure is if you share that input (as a mat file).
By the way, why write:
if (strcmpi(my_lit,form_uten_tiltak))
continue
end
disp('Tiltaket for sonen settes i gang')
when
if ~strcmp(my_lit, uten_tiltak)
disp('Tiltaket for sonen settes i gang');
end
is simpler and clearer.
Réponses (3)
Luna
le 11 Jan 2019
Try this below:
function where_is_solution_required(lit_kolonne)
form_uten_tiltak = 'SANDSTEIN';
for k=1:numel(lit_kolonne)
my_lit=lit_kolonne{k};
if ~(strcmpi(my_lit,form_uten_tiltak))
disp('Tiltaket for sonen settes i gang')
end
end
end
4 commentaires
Muazma Ali
le 11 Jan 2019
If you want to do it with continue, you should change the line lit_kolonne(k) with curly paranthesis {k} to reach the char array inside the cell, because your lit_kolonne is a cell array.
Currently, you are comparing a cell with a char with strcmp.
Also check your lit_kolonne if it is 4x1, or 1x4 cell array.
my_lit=lit_kolonne{k};
Please accept the answer if it helped you and you get the solution.
The code works with continue for below input that I have created.
lit_kolonne = {'a','b','SANDSTEIN','d'}';
Muazma Ali
le 11 Jan 2019
char array, cell array, double array are different things.
Continue is working in my machine with that code. Are you sure that SANDSTEIN is all upper case? Maybe yours is not working because of case sensitivity. Which version of Matlab you are using?
Read about strcmpi and strcmp.
Muazma Ali
le 11 Jan 2019
0 votes
5 commentaires
Luna
le 11 Jan 2019
function where_is_solution_required2(lit_kolonne)
form_uten_tiltak='SANDSTEIN';
for k=1:size(lit_kolonne,1)
my_lit=lit_kolonne(k);
if (strcmpi(my_lit,form_uten_tiltak))
continue
end
disp('Tiltaket for sonen settes i gang')
end
end
I saved above function without changing anything from your code, created below input:
lit_kolonne = {'a','b','sandstein','d'}'; % 4x1 cell array
I called the function
where_is_solution_required2(lit_kolonne)
The result is below:
>> where_is_solution_required2(lit_kolonne)
Tiltaket for sonen settes i gang
Tiltaket for sonen settes i gang
Tiltaket for sonen settes i gang
It works for me. Again I'm telling show us what input you are using!
Guillaume
le 11 Jan 2019
As I commented to the question a while ago, although badly written the original code does what it is supposed to do. Therefore the problem is most likely with the input which we have yet to see.
always use the one with i to be on the safe side.
That is a strange statement. The safe side may be to be case sensitive. Always use the one that is relevant to your code.
Muazma Ali
le 11 Jan 2019
Image Analyst
le 11 Jan 2019
Muazma, why are you refusing to attach your data in a .mat file with the paper clip icon like Guillaume requested?
I sense he's about to give up trying to help you and move on to easier questions.
Muazma Ali
le 11 Jan 2019
Muazma Ali
le 11 Jan 2019
0 votes
1 commentaire
Guillaume
le 11 Jan 2019
Please stop using Answers for comments
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!