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

Adam
Adam 2019년 1월 11일
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
Muazma Ali 2019년 1월 11일
hi,
the input is an array containing 4 cells, the third one named 'sandstein' and the other are named 'skifer'. Since the criteria is about 'sandstein' , occuring only once in the input array, I should only get the disp output three times not four, according to the 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
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.

댓글을 달려면 로그인하십시오.

답변 (3개)

Luna
Luna 2019년 1월 11일

0 개 추천

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

Muazma Ali
Muazma Ali 2019년 1월 11일
i will try this..but I used it because I wanted to learn using continue in my executions :)
I agree with that your suggetion seems to be easier
Luna
Luna 2019년 1월 11일
편집: Luna 2019년 1월 11일
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
Muazma Ali 2019년 1월 11일
let me explain further..in my script I have a dataset and I set lit_kolonne equal to one of the columns of the dataset ;
lit_kolonne= petrophysics.litologi;
here petrophysics.litologi contains a character array.
Luna
Luna 2019년 1월 11일
편집: Luna 2019년 1월 11일
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
Muazma Ali 2019년 1월 11일

0 개 추천

i know the difference between strcmpi n that without i.
always use the one with i to be on the safe side.
matlab 2018b

댓글 수: 5

Luna
Luna 2019년 1월 11일
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
Guillaume 2019년 1월 11일
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
Muazma Ali 2019년 1월 11일
I have no problem using strcmpi/strcmp
back to your question abt the input..have mistakenly written it as an answer to the question but can write it again here..:
the input is a character array from my script;
lit= petrofysikk.litologi;
as you can see it was part of a dataset earlier.
and lit is then sent as an input to the function.
Image Analyst
Image Analyst 2019년 1월 11일
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
Muazma Ali 2019년 1월 11일
its ok I have already got some suggetions to the question that I am going to try tomorrow. :)
right now I dont have the possibility to do what you are saying since I havent my computer right in front of ..I also think that I have explained it in detail how i have done it. Anyway thanks so far.

댓글을 달려면 로그인하십시오.

Muazma Ali
Muazma Ali 2019년 1월 11일

0 개 추천

as I wrote earlier I think the input is a character array from my script..that I set equal to column from my dataset , in my script like this:
lit=petrofysikk.litologi
where petrofysikk.litologi is a character array within the dataset petrofysikk.
And lit is then sent as an input to the function.

댓글 수: 1

Guillaume
Guillaume 2019년 1월 11일
Please stop using Answers for comments

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2019년 1월 11일

댓글:

2019년 1월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by