[absolute beginner] What is the purpose of strcat when used in this way?

Hello,
If I have a code that goes:
my_dir = char(InputtedFile{:,"VariableColumn"});
my_dir = strcat(my_dir,'\');
When I run the first line it is still the same result as if I run the second line with it. What is the purpose of this conceptually? In the first line of code I am specifying a column in a file I previously input and assigning it to a variable. I am as well a little unclear as to why char is used but I am still searching on that.
Thank you..

回答 (1 件)

Cris LaPierre
Cris LaPierre 2023 年 10 月 12 日
The explanation from the documention does a good job summarizing its function as "Concatenate strings horizontally". So it takes the first input and adds the second input to it. Here, that is a backslash.
We don't have your variable InputtedFile to work with, so I've replaced it.
my_dir = char(["Happy";"Birthday"])
my_dir = 2×8 char array
'Happy ' 'Birthday'
my_dir = strcat(my_dir,'\')
my_dir = 2×9 char array
'Happy\ ' 'Birthday\'
I don't know why you are using char. Why did you include it? What is the result if you remove it?
my_dir = ["Happy";"Birthday"];
my_dir = strcat(my_dir,'\')
my_dir = 2×1 string array
"Happy\" "Birthday\"
It looks like InputtedFile is a table. It might be simpler to extract your variable using dot notation (see here: https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-table.html).
my_dir = InputtedFile.VariableColumn

5 件のコメント

Dani
Dani 2023 年 10 月 12 日
Hi Cris,
I checked and "InputtedFile" refers to an excel sheet that was input previously and "Variable Column" is just an (empty) column inside that.
Does the code then mean it wished to space each then addition ? because of the '\'?
Cris LaPierre
Cris LaPierre 2023 年 10 月 12 日
編集済み: Cris LaPierre 2023 年 10 月 12 日
Looking at the code, it appears it is trying to build a directory path (e.g C:\Users\Me\Documents). You might consider looking into fullfile for that purpose.
The code used the values that were inputted to it. If my_dir is empty, then the result of the code will be a vector of backslashes.
my_dir = ''
my_dir = 0×0 empty char array
my_dir = strcat(my_dir,'\')
my_dir = '\'
Dani
Dani 2023 年 10 月 12 日
addpath and setting directories (input and output) and such were done prior to the code... I guess it would have been helpful to show the whole code. I am just trying to understand beyond what I am just seeing. So when I see
strcat(my_dir,'\')
I can look at it and thingk "hey that means xyz"
I am having to learn from someone elses pre-written code. I am having a bit of a hard time.
Cris LaPierre
Cris LaPierre 2023 年 10 月 12 日
In this case, you can think "strcat(A,B)" means "AB".
If you don't have the ability to talk with the person who wrote the code, you can build up a general understanding of MATLAB by going through some of the Onramps or other self-paced trainings you may have access to. You can access those here: https://matlabacademy.mathworks.com/
+1 to using fullfile. You can also use filesep to add the ending slash. That way the correct file seporator character will be added for the system.
my_dir = '2023Taxes';
fullfile(my_dir,filesep)
ans = '2023Taxes/'
Another benefit is that fullfile will not add an additional separator if there's one already present.
my_dir = '2023Taxes/'
my_dir = '2023Taxes/'
fullfile(my_dir,filesep)
ans = '2023Taxes/'

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

カテゴリ

ヘルプ センター および File ExchangeShare and Distribute Software についてさらに検索

タグ

質問済み:

2023 年 10 月 12 日

コメント済み:

2023 年 10 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by