NLed
NLed

Reputation: 1865

How to print a value as a variable from a cell in Matlab?

For

A=[1;3;5]

and

B=cell(7,1)

I have the following results stored in a cell

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

I would like to print the results in a way that a=1, b=3, and c=5. -- Basically assign each value in A to a variable.

How would I do this in Matlab ?


I am looking for a result which can be something like this :

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

Upvotes: 0

Views: 313

Answers (2)

H.Muster
H.Muster

Reputation: 9317

Let C be the array of letters you want to assign to the numbers in A. Then

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 '), '_', '')];

results in

str =

you can have a or b or c

If you want to create the responses to all fields in B at once, you can use

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

This results in

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'

A step by step explanation of this code is as follows:

% 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];

To get this working with complete words (as requested in the comments), you need to transform C into a cell array of strings and update the formula accordingly:

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 '), '_', '')];

This results in:

str =

you can have first or second or third

Upvotes: 1

devrobf
devrobf

Reputation: 7213

If I have understood properly, you want something like this:

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

The main bit of your question was how to assign each number to a letter (note: I know you asked to assign each one to a variable, but I don't think that's quite what you want.). This is done using a lookup table, called numToLetter, which has a stored at 1, b stored at 3, and c stored at 5. This way you simply use your input numbers as indices into this table. You could use this lookup table with a vector; for example:

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

Gives the output:

myLetters =

abbac

Upvotes: 1

Related Questions