How do I join a single string with multiple strings?

3 Ansichten (letzte 30 Tage)
Tom Wright
Tom Wright am 17 Okt. 2013
Kommentiert: Tom Wright am 17 Okt. 2013
Hi, I know I can do this with loops but there must be a nicer way.
basepath='/data/';
filebase='video_';
fileext='.avi';
files=['00a' '00b' '00c'];
I'd like the output to be the vector
['/data/video_00a.avi' '/data/video_00b.avi' '/data/video_00c.avi']
(although I'd also be happy with a vertical matrix.
Thanks Tom
  1 Kommentar
Matt Kindig
Matt Kindig am 17 Okt. 2013
Bearbeitet: Matt Kindig am 17 Okt. 2013
Are you sure you don't want 'files' to be a cell array? Because the way you've defined files, it will just concatenate 00a, 00b, etc. as one string. Observe:
files=['00a' '00b' '00c']
% files = '00a00b00c'
This is probably less useful to you. Instead, define 'files' as:
files = {'00a', '00b', '00c'}

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 17 Okt. 2013
Bearbeitet: Azzi Abdelmalek am 17 Okt. 2013
basepath='/data/';
filebase='video_';
fileext='.avi';
files={'00a', '00b', '00c'}
cellfun(@(x) sprintf([basepath filebase '%s' fileext],x),files,'un',0)
Look at
doc cell

Weitere Antworten (1)

Vivek Selvam
Vivek Selvam am 17 Okt. 2013
Here you go, Tom.
basepath = '/data/';
filebase = 'video_';
fileext = '.avi';
files = ['00a'; '00b'; '00c'];
n = size(files,1);
horzcat(repmat(basepath,n,1), repmat(filebase,n,1), files, repmat(fileext,n,1))

Kategorien

Mehr zu Convert Image Type finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by