How to Set Specific Arg in Function

37 ビュー (過去 30 日間)
Ted Baker
Ted Baker 2020 年 2 月 27 日
編集済み: Stephen23 2024 年 4 月 28 日
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.

採用された回答

Stephen23
Stephen23 2020 年 2 月 27 日
編集済み: Stephen23 2020 年 2 月 27 日
"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 件のコメント
Ted Baker
Ted Baker 2020 年 2 月 27 日
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. :)

サインインしてコメントする。

その他の回答 (1 件)

Vibhav
Vibhav 2024 年 4 月 26 日
Since R2021a, you can use named arguments in MATLAB:
https://www.mathworks.com/help/matlab/matlab_prog/namevalue-in-function-calls.html#
  1 件のコメント
Stephen23
Stephen23 2024 年 4 月 28 日
編集済み: Stephen23 2024 年 4 月 28 日
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).

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by