如何在Matlab中从单元格中将值打印为变量?

时间:2013-03-06 08:52:21

标签: arrays matlab cell

有关

A=[1;3;5]

B=cell(7,1)

我将以下结果存储在单元格中

[1]
[3]
[5]
[1;3]
[1;5]
[3;5]
[1;3;5]

我希望以printa=1b=3的方式c=5结果。 - 基本上将A中的每个值分配给变量。

我将如何在Matlab中执行此操作?


我正在寻找一个类似的结果:

" you can have a "
" you can have b "
" you can have c "
" you can have a or b "
.
.
etc

2 个答案:

答案 0 :(得分:1)

如果我理解得当,你想要这样的东西:

numToLetter = [ 'a', ' ', 'b', ' ', 'c' ];
B = { 1, 3, 5, [ 1; 3 ], [ 1; 5 ], [ 3; 5 ], [ 1; 3; 5 ] };

% Loop though each entry in our cell array
for i = 1 : length(B)

  fprintf(' you can have ');               % Print initial message

  % Loop though each vector element inside the B{i}
  for j = 1 : length(B{i})
    fprintf('%c', numToLetter(B{i}(j) ) )  % Use our numToLetter lookup table
                                           % to convert the number to a letter,
                                           % and print it out.

    if j ~= length(B{i})
      fprintf(' or ');                     % Print 'or' if there are more to come
    end
  end
  fprintf('\n');                           % New line
end

你问题的主要部分是如何将每个数字分配给一个字母(注意:我知道你要求将每个数字分配给一个变量,但我认为这不是你想要的。)。这是使用名为numToLetter的查找表完成的,其中a存储1b存储在3c已存储在5。这样,您只需使用输入数字作为此表的索引。您可以将此查找表与向量一起使用;例如:

myNumbers = [ 1 3 3 1 5 ];
myLetters = numToLetter(myNumbers)

给出输出:

myLetters =

abbac

答案 1 :(得分:1)

C成为您要分配给A中的数字的字母数组。然后

A = [1 3 5];
B = {[1]; [3]; [5]; [1;3]; [1;5]; [3;5]; [1;3;5]};
C = ['a', 'b', 'c']

k = 6; % indicates current line of B
str = ['you can have ' strrep(strrep(sprintf('_%c_', ...
    C(ismember(A, B{k}))'), '__', ' or '), '_', '')];

结果

str =

you can have a or b or c

如果您想一次 B 中的所有字段的回复,您可以使用

allStr = arrayfun(@(x) ['you can have ' strrep(strrep(sprintf('_%c_', ...
    C(ismember(A, B{x}))'), '__', ' or '), '_', '')], ...
    (1:length(B))', 'uniformoutput', false)

这导致

allStr = 

    'you can have a'
    'you can have b'
    'you can have c'
    'you can have a or b'
    'you can have a or c'
    'you can have b or c'
    'you can have a or b or c'

对此代码的逐步说明如下:

% which contents of A can be found in B?
idx = ismember(A, B{k})'; 

% to which letters do these indices correspond?
letters = C(idx);

% group the letters in a string embedded in '_' as place holders for later use
% by this, the places between letters will be marked with '__' and the places 
% at the beginning and the end of the string will be marked with '_'
stringRaw = sprintf('_%c_', letters); 

% replace each occurrence of '__' by ' or '
stringOr = strrep(stringRaw, '__', ' or ');

% replace each occurrence of '_' by ''
stringClean = strrep(stringOr, '_', ''); 

% add first half of sentence
stringComplete = ['you can have ' stringClean];

要使用完整的单词(根据评论中的要求),您需要将C转换为字符串的单元格数组并相应地更新公式:

A = [1 3 5];
B = {[1]; [3]; [5]; [1;3]; [1;5]; [3;5]; [1;3;5]};
C = {'first', 'second', 'third'}

k = 7; % indicates current line of B
str = ['you can have ' strrep(strrep(sprintf('_%s_', ...
    C{ismember(A, B{k})}), '__', ' or '), '_', '')];

这导致:

str =

you can have first or second or third