Row-wise concatenation of a cell array

Apologies for what may be a banal question, but I'm having trouble with the following:
I'm writing code that involves tracking paths and filenames for 1-100 files, which I'm doing with a 2 X N (N = number of files) cell array, where the first row contains the path(s) and the second row contains the filename(s). As entries in the same column represent a single file, I need a way to concatenate every pair of entries (:,N) to assemble full paths in another cell array. Any suggestions on how best to do this (without loops, which I'm trying to avoid, unless I shouldn't be)?
Thanks!
-Davis
Thanks!

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2011년 12월 22일

0 개 추천

try this code, I can not test it, now do not have access to MATLAB
data - your cell array (size 2xN)
out = arrayfun(@(i1)fullfile(data{:,i1}),(1:size(data,2))','un',0);

댓글 수: 5

Davis
Davis 2011년 12월 22일
Thanks a lot, that worked! Would you mind explaining how this works?
Andrei Bobrov
Andrei Bobrov 2011년 12월 22일
explaining with my english? :)
better please read:
about 'fullfile' http://www.mathworks.se/help/techdoc/ref/fullfile.html
about 'arrayfun' http://www.mathworks.se/help/techdoc/ref/arrayfun.html
about 'function_handle (@)' http://www.mathworks.se/help/techdoc/ref/function_handle.html
Daniel
Daniel 2020년 4월 2일
I created a repository in Github based on this solution. Ideally it should also account for more than 2 dimensions.
For a data cell containing {'File_name', 'File_extention'} which is Nx2, then you can create a single-column cell {'File_name.File_extention'} at Nx1 with
= arrayfun(@(row)[(data{row,:})],(1:size(data,1))','UniformOutput', false);
Sedo
Sedo 2023년 2월 4일
Thank you this worked for me!

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

질문:

2011년 12월 22일

댓글:

2023년 2월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by