Azzera filtri
Azzera filtri

whats wrong with this code?

1 visualizzazione (ultimi 30 giorni)
Muazma Ali
Muazma Ali il 11 Gen 2019
Commentato: Muazma Ali il 11 Gen 2019
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 Commenti
Muazma Ali
Muazma Ali il 11 Gen 2019
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
Guillaume
Guillaume il 11 Gen 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.

Accedi per commentare.

Risposte (3)

Luna
Luna il 11 Gen 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 Commenti
Muazma Ali
Muazma Ali il 11 Gen 2019
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 il 11 Gen 2019
Modificato: Luna il 11 Gen 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.

Accedi per commentare.


Muazma Ali
Muazma Ali il 11 Gen 2019
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 Commenti
Image Analyst
Image Analyst il 11 Gen 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
Muazma Ali il 11 Gen 2019
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.

Accedi per commentare.


Muazma Ali
Muazma Ali il 11 Gen 2019
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 Commento
Guillaume
Guillaume il 11 Gen 2019
Please stop using Answers for comments

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by