formatting string with vectors
5 ビュー (過去 30 日間)
古いコメントを表示
Hello,
As a part of my homework task, I need to write a function that formats an existing string with given variables which may be vectors or scalars and returns it in one cell array. for example,
num_participants = 3, experiment_num = 8:10
the output should look like, with no spaces:
exp experiment_number_sub10 participant number
exp8_sub101
exp8_sub102
....
exp_10sub103
and all of this output should be in one cell array. So I figured out two ways and then got stuck:
experiment_num = 8:10
num_participants = 3
f = cellstr(num2str((experiment_num)', 'exp%d_'))
f2 = cellstr(num2str(([1:num_participants])', 'sub10%d'))
This works perfectly fine but I couldn't find a way to concatanate the two cell arrays into one (not in two coloumns but in one column)
and
mat = [experiment_num',(1:num_participants)']
list = sprintf('exp%d_sub10%d\n', mat)
only my output, in this case, is not in the right order
list =
'exp8_sub109
exp10_sub101
exp2_sub103
'
though the columns themselves are aligned properly.
Your help is much appreciated (I read the documentation and googled anything I could, but I cannot understand how to proceed.
Thank you
0 件のコメント
回答 (1 件)
Bob Thompson
2018 年 6 月 11 日
You will probably have to run a for loop, but I usually like to just concatenate a string when I put in variables.
if true
experiment_num = 8:10;
num_participants = 3;
for k = 1:length(experiment_num);
list{k} = ['exp',num2str(experiment_num(k)),'_sub10',num2str(k)];
end
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Entering Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!