Borrar filtros
Borrar filtros

How to Set Specific Arg in Function

39 visualizaciones (últimos 30 días)
Ted Baker
Ted Baker el 27 de Feb. de 2020
Editada: Stephen23 el 28 de Abr. de 2024
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)
What am I doing wrong? Thanks in advance.

Respuesta aceptada

Stephen23 el 27 de Feb. de 2020
Editada: Stephen23 el 27 de Feb. de 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
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 comentario
Ted Baker
Ted Baker el 27 de Feb. de 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. :)

Iniciar sesión para comentar.

Más respuestas (1)

Vibhav el 26 de Abr. de 2024
Since R2021a, you can use named arguments in MATLAB:
  1 comentario
Stephen23 el 28 de Abr. de 2024
Editada: Stephen23 el 28 de Abr. de 2024
The function the OP was calling did not use name-value inputs, only positional inputs (specifically: the 12th input). That the OP did not understand the difference was the OP's problem in the first place, so this answer just continues the confusion rather than clarifying it.
Note that name-value inputs have been suported for as long as any function has been written to parse them, certainly much earlier than 2021a (as the date of the original question and answer reminds us).

Iniciar sesión para comentar.


Más información sobre String Parsing en Help Center y File Exchange.


Community Treasure Hunt

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

Start Hunting!

Translated by