mercredi 2 septembre 2020

Why does "if contains" not work when using a function output as string input for "contains"?

I have some files in a folder and try to reorganize them. I recycled a function which returns my new criteria by which I sort the files now. The function load_data returns a string. This string is then checked if it contains a certain substring.

for s = 1:subjects
    ref = load_data(s);
    …
    if contains(ref,'Pat_1')
        …
    elseif contains(ref,'Pat_2')
…

However when I use the code shown above the "contains" command seems to have problem with the ref variable. I wrote a test script which did exactly the same with the difference that the string was hand written and not imported with a function and the test script worked.

The picture shows the code from above as well as the workspace:

matlab

The script doesn't return any error, it just jumps into the else collumn which doesn't do anything. I wasn't sure if the output of a function would somehow take another format just by being an output and tried the convertsCharToString command because why not try it, same problem. Another idea was to use a "switch - case" approach but I am even less experienced with this and wasn't sure if you could use something like this:

switch ref
    case contains('Pat_1')
    ...

Aucun commentaire:

Enregistrer un commentaire