join 2 strings together to 1

조회 수: 23 (최근 30일)
Patrick Benz
Patrick Benz 2021년 3월 5일
댓글: Patrick Benz 2021년 3월 5일
Good Morning all,
i need help manipulating strings.
I know from practically every other program that you can put strings together in one way or another. So very simple, make 1 out of 2.
In Matlab I despair of it.I have a variable 'job' (contains something like: "Biegung_35_") and a string 'Evaluation.txt'. In order to import the content of the txt into matlab either via importfile or readtable, I would like to combine these two strings. I don't mean the kind of assembly that join or cat does, so that I have a 1x2 string array.
It should look like this 'Biegung_35_Evaluation.txt' in a single string.
Please help me

채택된 답변

Stephen23
Stephen23 2021년 3월 5일
편집: Stephen23 2021년 3월 5일
Your example data (you need to learn the difference between string arrays and character arrays):
A = "Biegung_35_"; % scalar string.
B = 'Evaluation.txt'; % character vector.
Here are six very basic approaches:
A+B % returns scalar string, uses overloaded string operation PLUS.
ans = "Biegung_35_Evaluation.txt"
strcat(A,B) % returns scalar string.
ans = "Biegung_35_Evaluation.txt"
sprintf("%s",A,B) % returns scalar string, which is what you asked for.
ans = "Biegung_35_Evaluation.txt"
sprintf('%s',A,B) % returns char vector, which is what your example actually shows.
ans = 'Biegung_35_Evaluation.txt'
[A{1},B] % returns char vector, uses char content of string element.
ans = 'Biegung_35_Evaluation.txt'
[char(A),B] % returns char vector, converts string to char.
ans = 'Biegung_35_Evaluation.txt'
The SPRINTF approach is the most robust as it lets you specify exactly the type of the output that you will get (and not rely on some magic coersion and an obscure list of type "rules" in the documentation).
Pointlessly complex approaches (if you see someone use these, run for the hills!):
join([A,B],'') % ugh, the stuff of nightmares.
ans = "Biegung_35_Evaluation.txt"
tmp = [A,B];
[tmp{:}] % comma-separated list to get content of string array.
ans = 'Biegung_35_Evaluation.txt'
cell2mat(cellfun(@char,{A,B},'uni',0)) % nested ouch.
ans = 'Biegung_35_Evaluation.txt'
compose("%s%s",A,B) % hmm.... maybe this isn't so bad afterall.
ans = "Biegung_35_Evaluation.txt"
  댓글 수: 1
Patrick Benz
Patrick Benz 2021년 3월 5일
Thx very much, this saves my day.
And to be honest, I didn't even think about simply using A+B for strings :D

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2021년 3월 5일
Read about join, strcat. You can also achieve the same using:
str = ['Biegung_35_' ,'Evaluation.txt'] ;
  댓글 수: 1
Stephen23
Stephen23 2021년 3월 5일
편집: Stephen23 2021년 3월 5일
Note that the first text sample given in the question is a scalar string, the second text is a character vector.
So using the examples from the question, this answer will coerce the character vector into a scalar string and return a 1x2 string array:
str = ["Biegung_35_" ,'Evaluation.txt']
str = 1×2 string array
"Biegung_35_" "Evaluation.txt"

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by