How to Set Specific Arg in Function

38 views (last 30 days)
Hi, I'm trying to use th function give at this link:
When I set the default values it works fine ie:
heatscatter(workingr, workingi,'', 'test.png');
However I would now like to add a title, but I cannot figure out how to do this. I've tried seraching for how to change/edit/give a specific argument but I can't find it. I've tried the following options but none seem to work:
heatscatter(workingr, workingi,'', 'test.png', 'title', 'testtitle');
heatscatter(workingr, workingi,'', 'test.png', 'title', testtitle);
heatscatter(workingr, workingi,'', 'test.png', title, testtitle);
I always get the error:
Nonfinite endpoints or increment for colon operator in index.
When I fill out the entire list of args as:
heatscatter(workingr, workingi,'', 'test.png','300', '10', '.', 0, 0, 'test', 'test2','title');
I can control all the parameters, except title - I get the following error:
Index exceeds the number of array elements (5).
Error in heatscatter (line 186)
title(title);
What am I doing wrong? Thanks in advance.

Accepted Answer

Stephen23
Stephen23 on 27 Feb 2020
Edited: Stephen23 on 27 Feb 2020
"I've tried seraching for how to change/edit/give a specific argument but I can't find it. I've tried the following options but none seem to work:"
You can't find it because it doesn't exist.
MATLAB function input (and output) arguments are positional, not named like you are trying.
Some functions are written to parse "name-value" pairs, which are simply two adjacent inputs defined as:
  1. the key name as string/character vector, and
  2. the value corresponding to that name.
But name-value pairs only work if that function was written to parse them, i.e. literally the author has to write code that checks if any of the inputs contain particular strings and value and then does something with them, or uses the inputparser, or something similar. The help and the code of that badly-written third-party function that you linked to makes it clear that it does not parse name-value pairs.
This means that the only way to provide its input arguments is by providing the arguments by position: its title input is the 12th input argument, therefore if you want to provide the title it must be the 12th input argument when you call the function. It really is that simple, and is exactly what is described in its help.
"What am I doing wrong?"
That error this is caused by the fact that its author defined title as a variable in the function definition, so that when the code gets to this line
title(title);
of course both of those title's refers to that variable (and not the inbuilt function of the same name) which MATLAB interprets as an attempt to index into that variable (for historical reasons interpreting the character vector that you provided by its ASCII code values as the indices). Basically the code is doing this:
>> A = 'ABC';
>> A(A)
Index exceeds matrix dimensions.
Solution: rename the variable from title (bad idea) to something like titlestr
A trivial bug that indicates that the code was not tested at all, it is unlikely to be free from other bugs, and its author makes basic MATLAB mistakes without noticing. The FEX comments are worth reading too.
  1 Comment
Ted Baker
Ted Baker on 27 Feb 2020
Thank you very much for your detailed reply, Stephen. I appreciate it very much.
Although I am at the start of my MATLAB journey, I hope to write better functions someday. :)

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!