How to concatenate array with delimeter?

1 回表示 (過去 30 日間)
Laurensius Christian Danuwinata
Laurensius Christian Danuwinata 2015 年 12 月 22 日
a1 = [0 32];
c1 = ([31 63]);
d1 = unique([a1 c1]);
first = ['[' num2str(d1(1)) ':' num2str(d1(2)) ']'];
second =['[' num2str(d1(3)) ':' num2str(d1(4)) ']'];
preresult= [ first second];
result = ['{' preresult '}'];
%result = {[0:31][32:63]}
How can I make this code worked more generally, for any values of a1 and c1? I've tried with this :
newArray =[];
for i=1:length(a1)
newArray(length(newArray)+1)= [ '[' num2str(a1(i)) ':' num2str(c1(i)) ']' ]
end
But it said Subscripted assignment dimension mismatch. Can someone help me maybe? :(
  1 件のコメント
jgg
jgg 2015 年 12 月 22 日
Are you sure this is doing what you want it to? The matrix [0 32] is not the same as the matrix [0:32]?

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

採用された回答

Guillaume
Guillaume 2015 年 12 月 22 日
This looks to be related to your other question, so I'll reiterate here what I said there:
The bit pattern is not made up of strings, you're barking up the wrong tree. It is a cell array of double vectors and it's simply:
result = arrayfun(@(s, e) s:e, a1, c1, 'UniformOutput', false);
Or if arrayfun is too dauting for you, with a loop:
result = cell(size(a1));
for iter = 1:numel(a1);
result{iter} = a1(iter) : c1(iter);
end
  1 件のコメント
Laurensius Christian Danuwinata
Laurensius Christian Danuwinata 2015 年 12 月 23 日
Thanks Guillaume, I did this in the end : for iter = 1:numel(a);
result{iter} = ['[' num2str(a(iter)) ':' num2str(c(iter)) ']'];
end
HORE = char(['{' result '}']);
set_param('can_examples/Subsystem/CAN bit-packing 1','bitpatterns', HORE);
and it worked, but just for the first iteration, afterwards it said,
Error evaluating 'MassCallback' ...; Field assignment to a non-structure array object.
If you don't mind, I can also send the files to you, regardless,
it was the very big help from you, thanks once again :D

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by