フィルターのクリア

[ABSOLUTE BEGINENR] char(inputdlg) or just inputdlg same result? why?

2 ビュー (過去 30 日間)
Dani
Dani 2023 年 10 月 11 日
編集済み: Dyuman Joshi 2023 年 10 月 11 日
Hi, this is a concept question.
When I run
agedirectory = char(inputdlg({'Emter your age'}))
and
agedirectory = inputdlg({'Emter your age'})
I still get the same outcome. If this is the case, why do we use char? is it required?
  1 件のコメント
Stephen23
Stephen23 2023 年 10 月 11 日
編集済み: Stephen23 2023 年 10 月 11 日
"If this is the case, why do we use char?"
I would not use CHAR like that, nor do any of the INPUTDLG examples show CHAR being used like that:
Why would you?
"is it required?"
No. The robust approach is to access the cell array content, just as the documentation shows, as well as checking for the special cases that occur when e.g. the user closes the dialog box.

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

回答 (2 件)

Dyuman Joshi
Dyuman Joshi 2023 年 10 月 11 日
編集済み: Dyuman Joshi 2023 年 10 月 11 日
"I still get the same outcome."
They might look same but the outputs are different.
"is it required?"
That depends on what is the expected/required format/data-type of the output.
However, even if the expected/required output is a character array, a better approach is to use indexing to access the data in the cell array.
Using only inputdlg(), the output is a cell array of character vector (as mentioned in the documentation of inputdlg as well) -
%Running the code with input provided as 25
agedirectory = inputdlg({'Emter your age'})
%Output
agedirectory =
1x1 cell array
{'25'}
Using char() on the output of inptdlg() converts the data into character array*.
agedirectory = char(inputdlg({'Emter your age'}))
%Output
agedirectory =
'25'
  2 件のコメント
Stephen23
Stephen23 2023 年 10 月 11 日
"Using char() on the output of inptdlg() converts the data into character vector."
Strictly speaking, into a character matrix (with zero or more rows).
Indexing into the cell array is the clearest approach.
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 11 日
編集済み: Dyuman Joshi 2023 年 10 月 11 日
Yes, you are right @Stephen23. It seems like I wrote it in the flow of the previous sentence.
I've corrected it now.
I think it's time for TMW to introduce strikethrough in MATLAB Answers, so that it becomes clear to show corrections.

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


Steven Lord
Steven Lord 2023 年 10 月 11 日
Let's take a look at the documentation page for the inputdlg function for information about what it returns. [If you want to open these documentation pages in MATLAB, not in your web browser, use the doc function.]
The first syntax (which is what your call looks like) is described in the first entry of the Description section.
"answer = inputdlg(prompt) creates a modal dialog box containing one or more text edit fields and returns the values entered by the user. The return values are elements of a cell array of character vectors. The first element of the cell array corresponds to the response in the edit field at the top of the dialog box. The second element corresponds to the next edit field response, and so on."
answer is a hyperlink that leads to more information about that output argument in the Output Arguments section of the documentation page. If you click it, the entry for the output argument is:
"Returns a cell array of character vectors containing one input per edit field, starting from the top of the dialog box. Use the str2num function to convert space-delimited and comma-delimited values into row vectors, and semicolon-delimited values into column vectors. For an example, see Convert Input to Numeric Values."
Now let's look at the documentation page for the char function and see if the Input Arguments section describes what it does to "a cell array of character vectors". In fact it does; see the table in the description of the A input argument.
You said "I still get the same outcome." Do you? We can't run code that uses inputdlg in MATLAB Answers (it doesn't allow interactive functions) but when I run your two sections of code in MATLAB on my desktop there is a slight difference in the outputs.
>> agedirectory = char(inputdlg({'Emter your age'}))
agedirectory =
'42'
>> agedirectory2 = inputdlg({'Emter your age'})
agedirectory2 =
1×1 cell array
{'42'}
>> whos
Name Size Bytes Class Attributes
agedirectory 1x2 4 char
agedirectory2 1x1 108 cell
They appear very similar, and in many situations you can use agedirectory and agedirectory2 interchangeably, but they are different types. The variable agedirectory is a vector of characters while the variable agedirectory2 is a cell array with 1 cell, the contents of which is a vector of characters. In fact the contents of the cell in agedirectory2 is exactly the same as the contents of agedirectory2.
>> contentsOfCell = agedirectory2{1}
contentsOfCell =
'42'
>> isequal(agedirectory, contentsOfCell)
ans =
logical
1
I wouldn't use char in this case, as it can add a lot of spaces to the char matrix if you're asking for two quantities and they're different lengths. If you need to not have a cell array I'd instead call the string function to turn the cell array of character vectors into a string array.
>> ageAndName = inputdlg({'Enter your age', 'Enter your name'})
ageAndName =
2×1 cell array
{'42' }
{'Steven'}
>> c = char(ageAndName)
c =
2×6 char array
'42 '
'Steven'
>> s = string(ageAndName)
s =
2×1 string array
"42"
"Steven"
Note the extra spaces after 42 in c, and that there are no extra spaces after 42 in s. This is relevant if I index into c and s to extract the age.
>> age1 = c(1, :)
age1 =
'42 '
>> age2 = s(1, :)
age2 =
"42"
  2 件のコメント
Dani
Dani 2023 年 10 月 11 日
編集済み: Dani 2023 年 10 月 11 日
Update: when I tried removing "char" from the original code I am to learn from I noticed when processing the next line of code there was an error:
age_dir = (inputdlg({'Enter the directory for both age files:'}, 'Input directory for age files'));
AgeFile = readtable(fullfile(age_dir,'agefile.txt'),'Delimiter','\t');
When char is removed I get:
Error using readtable
"filename" must be a string scalar or character vector.
and when I put the char back to original, char(inputdlg)
it works again
So, because of this I can say I learned
  • Char ensures the stored variable becomes a character vector and not a cell array. This is important because if we remove “char” from the code, we get an error when processing the next line.
correct?
Stephen23
Stephen23 2023 年 10 月 11 日
編集済み: Stephen23 2023 年 10 月 11 日
" This is important because if we remove “char” from the code, we get an error when processing the next line. correct?"
No.
Avoid CHAR like that.
As everyone here advised/showed, acess the content of the cell array using indexing.
"Char ensures the stored variable becomes a character vector and not a cell array."
It is, as Steven Lord explained, a bad way to handle the output from INPUTDLG. It will works in some limited circumstances, but is a) does not generalize to other situations using INPUTDLG (e.g. more inputs, inputs different lengths, user cancel) and b) leads you down a learning dead-end and c) is like using a hammer to crack a nut. In contrast using indexing is neat, simple, and makes the intent obvious... which later (as you gain more experience) you will learn is very important when debugging and maintaining code. Indexing is a useful skill you need to learn and understand when using MATLAB. Using CHAR like that is not.
Avoid CHAR like that, use indexing.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by