Runtime error, syntax warning: setting string, regexp strmatch

Thank you for writing us about your problem with using :octave: GNU Octave. To help us help you, please answer the following questions, if applicable.

Problem description:

runtime error:

octave:1> drocweds
warning: suggest parenthesis around assignment used as truth value near line 106, column 7 in file ‘/Users/bgold/Documents/AcuamarkDx2020/drocweds.m’
i = 1
j = 1
k = 1
error: drocweds: =: nonconformant arguments (op1 is 1x1, op2 is 1x6)
error: called from
drocweds at line 107 column 14
syntax warning:
octave:63> while (i <= 150)

if (i = 1)
disease(i) = n;
continue
else if (rem (i, 2) == (0))
warning: suggest parenthesis around assignment used as truth value
disease(i) = m;
continue
else (rem (i, 3) == (0))
disease(i) = n;
++i
end
end
flowchart at: 3D ROC flowchart
code at: 3D-ROC latest code

My system

  • OS: Mac OS Catalina Version 10.15.7
  • Octave version: e.g. Version 5.2.0 installed using homebrew
  • Installation method: installed using homebrew

your line:
if (i = 1)
should be:
if (i ==1)
What you did was set i to one. but you wanted to test it i was one.

Thank you for that suggestion, Doug.
Unfortunately, that didn’t solve it.
Here is what happens runtime:

octave:63> drocweds
i = 1
j = 1
k = 1
error: drocweds: =: nonconformant arguments (op1 is 1x1, op2 is 1x6)
error: called from
drocweds at line 107 column 14
octave:63>

code was altered as per recommendation to:

98 #####################################
99 # initialize fraction increment above minimum value
100 #########################
101 i = 1
102 j = 1
103 k = 1
104 # start first while loop
105 while (i <= 150)
106 if (i == 1)
107 disease(i) = n;
108 continue
109 else if (rem (i, 2) == (0))
110 disease(i) = m;
111 continue
112 else (rem (i, 3) == (0))
113 disease(i) = n;
114 ++i
115 end
116 end

please advise if I did not properly understand your suggestion.

Bert Gold, PhD, San Mateo, CA

The error is quite simple: You want assign a vector called “n” (dimension 1x6) to a single entry i of another vector disease. This is in general not possible.

disease = rand (1, 5);
n = 1:6;
disease(3) = n

error: =: nonconformant arguments (op1 is 1x1, op2 is 1x6)

Probably something went wrong with the creation of n, but the code snippets do not show how n is created or altered until line 107.

Hello Kai,

Thank you very much for your comments!
The entire program is here:
https://github.com/bgold04/3DROC-simulation

the string variables m and n are declared on line 19 and 20:

19 m = (“affect”);
20 n = (“normal”);

the function disease is declared on lines 41 - 43

41 function disease
42 disease = regexp(’\w{6}’)
43 endfunction

I would be much obliged if you would take a look at the whole script and see
if you can make suggestions to reconcile array sizes and types.

Best,

Bert Gold
San Mateo

function disease
disease = regexp(’\w{6}’)
endfunction

I think you want to create cell strings, e.g. {"cell string"}.

The brackets () around the “string” (array of characters is a better description in Octave) have no effect, it remains a 1x6 array.

>> size (((("affect"))))
ans =

   1   6

>> size ({"affect"})
ans =

   1   1

Changing everything to proper cell strings might cause many other errors, but this is a major re-design decision that might pay off. Note that in Matlab "string" is an 1x1 object, while in Octave "string" has roughly the semantic as 'string' in Matlab.

Hi Kai,

I had the program with those cell strings in it, just as you’ve specified, a few versions back… So, I would really appreciate it if you’d look at the github program and tell me what you would change and I promise to work on that tomorrow morning my time.

Thank you for your help!

Bert

Your project looks important, but my interest (time) for your project is rather limited.

After having a small look into your code, there are several potential flaws (coding style, no vectorization, several functions in a single script). Your project should familiarize more with Octave programming or hire external help. But for particular questions regarding Octave programming itself, this forum always welcomes your questions :slightly_smiling_face:

Hi!

I totally get it… I have been programming for 50 years, but I only learned Octave for Andrew Ng’s coursera.
Here is where I am stumped now… any help would be welcomed. This is probably just some dumb mistake.
(oh, I stuck the printing columns in for troubleshooting)

#variables and functions
#Having trouble with the following snippet:
m = {“affect”};
n = {“normal”};
function disease
disease = regexp{’\w{6}’}
endfunction

I do not fully understand the purpose of the code snippet. It seems you want to define a function “disease”, that extracts the first 6 characters from something (maybe “m”, “n”, or both?). Can you describe what the code snippet should do?

Regarding functions, unlike in other programming languages, in Octave the name of the function and the variable name, storing the returned value, are at best different, see https://octave.org/doc/v5.2.0/Defining-Functions.html Another good practice is to store a single function in a single file. Here an example of adding both inputs.

function return_value = disease (input_parameter_1, input_parameter_2)
  return_value = input_parameter_1 + input_parameter_2;
endfunction

call from Octave (script [after the function definition] or command line) with

some_val = disease (1, 2)

Still having string manipulation syntax problems… here is where I am now. Any help much appreciated (yes, I have made good progress in other parts of the script):

    Runtime error:

octave:1> fri9
parse error near line 93 of file /.../fri9.m

  syntax error

>>>   disp ("f is odd")
         ^

#####################################
# create truth dataset
#########################
# start disease truth loop
[disease(f)] = [n];
f = [1 : 1 : 150]; #integer counter and increment for disease assignment
f = 1;
disp ({disease(f)})
while (f < sample)
s = (-1)^f;
if ((f <= (2/3)*(sample)) & (s = -1)
  ({"affect"}) == cellstr (disease(f))
  disp ("f is odd")
else
  ({"normal"}) == cellstr (disease(f))
disp ("f is even")
f = f + 1;
endif
end
####################################################

To be honest, I don’t understand what your code is supposed to do. In particular the first line in the if and else branch, respectively.
The syntax error is probably because a closing bracket is missing in the if condition? Or is the first line in the if branch supposed to be part of the condition? In that case, you might be missing a Boolean operator connecting the two lines, too?
In any case, I don’t think this is valid syntax. Did you mean to write strcmp (disease(f), "affect")?
No idea what the first line in the else branch is supposed to do…

Hi Markus,

Thanks for your interest! To be clear, I am an academic. Project is under General Public License with the aim of publication for the masses. Project is applicable to COVID-19 diagnostics and other **diagnostic tests. Project is available to be viewed at: github bgold04/3DROC-simulation. Sorry for any commercial marks on my communications; these were inadvertent and I will try never to have that happen again.

Point of the overall script is to be able to visualize the effect of prevalence of a disease in a given population on the efficiency of the test . Test efficiency is adjudged through the drawing of a Receiver-Operator Characteristic (ROC) curve.

Other interesting variables are the sensitivity (ability of a test to identify true positives) and specificity (ability of a test to identify true negatives). The simulation will permit a choice of how many individuals are in the cohort to be tested, what percent of them truly have disease , and how good the test is.

Aim of the difficult section is to ASSIGN values to function disease(f) as “normal” or “affect”.

In response to earlier suggestions:

function disease
  disease = (({n}) | (({m})))
endfunction

where:

m = {"affect"};
n = {"normal"};
f = [1 : 1 : 150]; #integer counter and increment for status assignment

the point of the section posted earlier was to set disease(f) (where f is an integer) equal to a string, m or n.


The difficult bit is reprinted below, with today’s error (closely related to earlier error) listed first:

octave:1> sat3

parse error near line 92 of file /sat3.m

syntax error

>>> ({"affect"}) == cellstr (disease(f)) disp ("f is odd");
 carat is exactly beneath "p" in "disp"^

Today’s code in the difficult part:

#####################################
# create truth dataset
#########################
# start disease truth loop
[disease(f)] = [n];
f = [1 : 1 : 150]; #integer counter and increment for disease assignment
f = 1;
disp ({disease(f)})
while (f < sample)
  s = (-1)^f;
  if ((f <= (2/3)*(sample)) & (s = -1)
    ({"affect"}) == cellstr (disease(f)) disp ("f is odd"); **## EFFORT to ASSIGN disease(f) = m = "affect" and display result.**
  else
    ({"normal"}) == cellstr (disease(f)), disp ("f is even"), f = f + 1;
  endif
endwhile
############################################

**Entire project is available to be viewed at: github bgold04/3DROC-simulation

Thank you, Octave-community!

Bert Gold, PhD, FACMGG

@bgold04 Your post has been edited by me and moved to this topic in the hope to help you. Please do not start new topics about the same project. I know that your project is important to you and Octave/Matlab is not your “native” programming language, why did you choose that language for your implementation?

Despite all advises (from other academics familiar with Octave/Matlab programming :innocent: ) your Octave code does not improve to a level of having studied a small Octave/Matlab tutorial, which would answer many simple questions of that language: how to define functions, how to assign values, how to compare strings, basic programming pattern, etc.

Like Markus said, we are clueless what your code is supposed to do. If you want to run your code before the end of the pandemic, I suggest to take a day or two to study some simple tutorial of Octave or Matlab. In economical terms, the time spent fixing “simple” errors of the current project (11 days ago) already exceeds the average Octave/Matlab training time for a student.

I’m pretty sure that Octave is able to do what you are planning to do. But you should definitely spend the time to read the language basics.

I’m not sure what that is supposed to mean. You cannot “assign” values to a function. (That would shadow the function definition.)
You can assign values to variables. Or you can have functions return (multiple) output values.

I’m also not sure which language this is. It’s not Octave syntax…
What is that function supposed to do? As it is, it does nothing AFAICT. (Leaving apart that it is probably invalid syntax.)